UNIT-II Notes Java Script
UNIT-II Notes Java Script
****ARRAY:
- Array is used to store set of values in a single variable name.
- Array is a special variable which can hold more than one value at a time.
####Declaration of Array:
Example:-
<html>
<body>
</script>
</body>
</html>
Example:
<html>
<body>
a[0]=10;
a[1]=20;
a[2]=30;
a[3]=40;
a[4]=50;
=>"+a[0]);
</script>
</body>
</html>
Example:
<html>
<body>
for(i=0;i<5;i++)
for(i=0;i<5;i++)
document.write(a[i]+" ");
</script>
</body>
</html>
Example:-
<html>
<body>
for(i=0;i<5;i++)
var sum=0;
for(i=0;i<5;i++)
sum=sum+a[i];
</script>
</body>
</html>
Example:-
<html>
<body>
for(i=0;i<5;i++)
for(i=0;i<5;i++)
document.write(a[i]+" ");
for(i=a.length-1;i>=0;i--)
document.write(a[i]+" ");
</script>
</body>
</html>
================================================================
- Push method is used to add new item to the end of the array and it will
return new length of array.
- Syntax:
ArrayObjName.push(list of elements)
- If you want to add new element at the beginning of an array then we can use
unshift() method array.
- The value of length property of an array can be used as the index for the new
array element.
- Example:
<html>
<body>
a[0]=10;
a[1]=20;
a[2]=30;
a.push(40,50);
a.unshift(5);
a[a.length]=60;
</script>
</body>
</html>
================================================================
- Syntax:
ArrayObjName.sort()
- Example:
<html>
<body>
a.sort();
a.reverse();
b.sort();
c.sort();
</script>
</body>
</html>
================================================================
- Sometime there is need to combine all elements of array into a single string.
- For example:
- In this method we can combine two or more array elements together but its
is separated by comma.
- Syntax:
ArrayObjName.concat();
ArrayObjName.concat(ArrayObj2,ArrayObj3...);
- Syntax:
ArrayObjName.join([separator])
- slice() method:
- By using this method we can retrieve any particular elements list from the
given array.
- Syntax:
ArrayObjName.slice(startindex,endindex-1);
-Example:
<html>
<body>
//join() method
var s1=items.join()
document.write(s1);
var s2=items.join('-')
document.write("<br>"+s2);
//concat() method
var s3=items.concat();
document.write("<br>"+s3);
var s4=items.concat(num);
document.write("<br>"+s4);
//slice() method
document.write("<br>"+a.slice(2,7));
</script>
</body>
</html>
- Example:
<html>
<body>
//join() method
var s1=num.join();
var s2=num.join('-');
//concat() method
var s3=items.concat();
var s4=items.concat(lang);
//slice() method
var s5=a.slice(3,8);
</script>
</body>
</html>
/*
output
=======
S1 => One,Two,Three
S2 => One-Two-Three
S3 => Pen,Pencil,Eraser
S5 => 40,50,60,70,80
*/
================================================================
####: Changing elements of an Array:
- Example:
<html>
<body>
a.shift()
a.pop();
</script>
</body>
</html>
<html>
<body>
for(i=0;i<N;i++)
for(i=0;i<N;i++)
document.write(a[i]+"\t");
for(i=N-1;i>=Loc-1;i--)
a[i+1]=a[i];
a[Loc-1]=x;
N++;
for(i=0;i<N;i++)
document.write(a[i]+"\t");
</script>
</body>
</html>
<html>
<body>
for(i=0;i<N;i++)
for(i=0;i<N;i++)
document.write(a[i]+"\t");
for(i=Loc-1;i<N-1;i++)
a[i]=a[i+1];
N--;
for(i=0;i<N;i++)
document.write(a[i]+"\t");
</script>
</body>
</html>
================================================================
#### Function:
- Function is a part of main program.
- Function is a block of statements that will help us to solve the particular task.
function function_name()
//block of code
<html>
<body>
function Welcome()
Welcome();
</script>
</body>
</html>
function function_name(Parameter-list)
//block of code
<html>
<body>
function Addition(a,b)
document.write("Addition
of two numbers="+(a+b));
Addition(100,200);
</script>
</body>
</html>
- Example-1:
<html>
<body>
var no;
if(no%2==0)
document.write("Number is EVEN");
else
document.write("Number is ODD");
</script>
</body>
</html>
Example-2:
<html>
<body>
var result=no*no;
</script>
</body>
</html>
Example-3:
<html>
<body>
var area,radius;
area=(3.14*radius*radius);
return area;
document.write("Area of Circle="+z);
</script>
</body>
</html>
================================================================
###STRING:
- String is a collection of characters which contains alphabets, numbers or
special symbols.
- Example:
- Example:
var s1="VJTech";
var s2='VJTech';
***Methods:
<html>
<body>
document.write("Length of
String="+str1.length);
</script>
</body>
</html>
2) Concatenation of String:
I) By using + operator
var str1="VJTech";
var str2="Academy";
var str3=str1+str2;
str3="VJTechAcademy"
var str3=str1.concat(str2);
<html>
<body>
var str1="VJTech";
var str2="Academy";
var str3=str1+str2;
document.write("Concatenated
String="+str3);
var s3=s1.concat(s2);
document.write("<br>Concatenated
String="+s3);
</script>
</body>
</html>
- We already aware about that every characters of string has associated its
own index.
- By using charAt() method, we can retrieve any particular character from the
String.
<html>
<body>
var str1="VJTech";
document.write("Character
present at index 0="+str1.charAt(0));
document.write("<br>Character
present at index 1="+str1.charAt(1));
document.write("<br>Character present
at index 2="+str1.charAt(2));
document.write("<br>Character present
at index 3="+str1.charAt(3));
document.write("<br>Character present
at index 4="+str1.charAt(4));
document.write("<br>Character present
at index 5="+str1.charAt(5));
</script>
</body>
</html>
- In JavaScript, we have two methods that can be used for retrieving the
position of particular character in given string.
- search() - this method search string for the specified value and it will return
the position of match. It will return -1 if character is not present in given string.
<html>
<body>
var L1=str1.indexOf('T');
var L2=str1.search('Academy');
document.write("<br>'Academy'
found at position="+L2);
</script>
</body>
</html>
5) Dividing Text:
- The concat method is used to combine two string object together whereas
split() method is used to divide given string on the basis of provided separator.
- Syntax:
StringObjName.split(Separator,limit);
- Where:
- Example:
<html>
<body>
var s1=str.split();
document.write(s1);
var s2=str.split("o");
document.write("<br>"+s2);
document.write("<br>"+s3);
</script>
</body>
</html>
6) Copying a Substring:
I) substring():
- this method will retrieve the characters between start index and end index.
- Syntax:
StringObjName.substring(startindex,endindex);
- This method will retrieve the characters which are present between
startindex to endindex-1;
II) substr():
- This method will extracts the charcaters from the string which is starting from
startindex and end with specified length.
-Syntax:
StringObjName.substr(startindex,length);
- Example:
<html>
<body>
document.write(str.substring(16,25));
document.write("<br>"+str.substring(16));
document.write("<br>"+str.substr(16,9))
</script>
</body>
</html>
- JavaScript provides two methods which will helps us to manipulate the case
of string:
I) toLowerCase():
- This method is used to convert all characters of given string into Lower Case.
II) toUpperCase():
- This method is used to convert all characters of given string into Upper Case.
-Example:
<html>
<body>
document.write("Lower
case:"+str.toLowerCase());
document.write("<br>Upper
case:"+str.toUpperCase());
</script>
</body>
</html>
- In this section ,we can see how to convert given string into number.
- following are the methods which we can use for converting string to number:
I) parseInt()
- Example:
var str='123';
II) parseFloat():
- Example:
var str='123.45';
III) Number():
- This method will work for complete number and decimal number.
-Example:
var str1='98';
var str2='157.98';
I) toString():
- Example:
var num1=100;
var num2=145.50;
- Example:
var num=100;
- In this section, we can retrieve ASCII value of given character and also we can
try to retrieve character from given ASCII.
I) charCodeAt():
- Syntax:
StringObjName.charCodeAt([position]);
- For example, suppose given character is 'A' then by using above method we
will get its corresponding ASCII code 65.
II) fromCharCode():
- For example, suppose given ASCII value is 65 then then by using above
method we will get its corresponding character 'A'.
<html>
<body>
document.write("ASCII Value of
A="+str.charCodeAt(7));
document.write("<br>Character Value of
ASCII 65="+String.fromCharCode(65));
</script>
</body></html>
VJTech Academy…