Chapter 02
Array , Function &
String
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<html>
<body>
<script>
Output:
Fan
var products = new Array(); Pizza
products[0] = 'Soap '; Soap
Water
products[1] = 'Water';
products[2] = 'Pizza';
products[3] = 'Fan';
products.sort();
for (var i = 0; i < products.length; i++)
{
document.write(products[i] + '<br>');
}
</script></body>
19 </html>
20
Ascending sort
arr.sort(function(a,b){return(a-b)});
Descending sort
arr.sort(function(a,b){return(b-a)});
21
22
<html>
<body>
<script>
var fruits = ["Banana", "Watermelon", "Chikoo", "Mango", "Orange", "Apple"];
fruits.sort();
document.write(fruits+"<br>");
fruits.reverse();
document.write(fruits+"<br>");
</script>
</body> </html>
Output:
23
Apple,Banana,Chikoo,Mango,Orange,Watermelon
Watermelon,Orange,Mango,Chikoo,Banana,Apple
24
Example:
var CO_Subject = ["PHP", "CSS", "Java"];
var Math_Subject= ["Applied Math", "Elements of Maths"];
var subjects = CO_Subject.concat(Math_Subject);
document.write(subjects);
Output:
PHP,CSS,Java,Applied Math,Elements of Maths
25
26
27
<html>
<body>
<script> Output:
var products = new Array(); Car,Water,Soap,Pizza
products[0] = 'Car '; Car Water Soap Pizza
products[1] = 'Water';
products[2] = 'Soap';
products[3] = 'Pizza';
var str = products.concat();
document.write(str);
document.write('<br>');
var str = products.join(' ');
document.write(str);
28
</script></body></html>
29
30
31
32
33
The splice() method can be used to add new items to
an array, and removes elements from an array.
Syntax:
arr.splice(start_index,removed_elements,list_of_elements
_to_be_added);
Parameter:
•The first parameter defines the position where new
elements should be added (spliced in).
•The second parameter defines how many elements
should be removed.
•The list_of_elements_to_be_added parameter define
34 the new elements to be added(optional).
<html>
<body>
<script>
var fruits = ["Banana", "Watermelon", "Chikoo", "Mango", "Orange","Apple"];
document.write(fruits+"<br>");
fruits.splice(2,2, "Lemon", "Kiwi");
document.write(fruits+"<br>");
fruits.splice(0,2); //removes first 2 elements from array
document.write(fruits+"<br>");
</script>
</body>
</html>
Output:
Banana,Watermelon,Chikoo,Mango,Orange,Apple
Banana,Watermelon,Lemon,Kiwi,Orange,Apple
Lemon,Kiwi,Orange,Apple
35
The slice() method slices out a piece of an array into a
new array.
Syntax: arr.slice(array starting from array element 1);
Parameter:
•slices out a part of an array starting from array element 1.
36
<html>
<body>
<script>
var fruits = ["Banana", "Orange", "Lemon", "Apple", "Mango"];
document.write(fruits);
var citrus = fruits.slice(2);
document.write("<br>"+citrus);
</script>
</body>
</html>
Output:
Banana,Orange,Lemon,Apple,Mango
Lemon,Apple,Mango
37
38
39
40
41
42
43
44
45
46
47
Example of Local Variable:
function myFunction()
{
var carName = "Volvo";
// code here CAN use carName
}
48
Example of Global Variable:
var carName = "Volvo";
// code here can use carName
function myFunction()
{
// code here can also use carName
}
49
50
51
52
53
54
55
56
57
58
59
60
x=areaRect(6,8);
document.write(x);
</script>
</body>
</html>
output: 48
61
In above program areaRect (6,8) calls the function &
passes values 6 & 8.
The function calculates the area & returns it
Now, after the function has returned a value,
areaRect(6,8) is 48 is assigned to x
The statement document.write(x) will print 48 in the
webpage
62
63
String Properties
Sr. No. Property Description
1 constructor This property returns a reference to
the string function that created the
object
2 length Returns the length of a string.
3 prototype Allows you to add new properties
and methods to a String object.
64
String Methods:
65
Methods Description
charAt() It provides the char value present at the specified
index
charCodeAt() It provides the Unicode value of a character present
at the specified index.
concat() It provides a combination of two or more strings.
indexOf() It provides the position of a char value present in the
given string.
lastIndexOf() It provides the position of a char value present in the
given string by searching a character from the last
position.
search() It searches a specified regular expression in a given
string and returns its position if a match occurs.
match() It searches a specified regular expression in a given
string and returns that regular expression if a match
occurs.
replace() It replaces a given string with the specified
replacement.
66
Methods Description
substr() It is used to fetch the part of the given string on the
basis of the specified starting position and length.
substring() It is used to fetch the part of the given string on the
basis of the specified index.
slice() It is used to fetch the part of the given string. It allows
us to assign positive as well negative index.
toLowerCase() It converts the given string into lowercase letter.
toUowerCase() It converts the given string into uppercase letter.
toString() It provides a string representing the particular object.
valueOf() It provides the primitive value of string object.
split() It splits a string into substring array, then returns that
newly created array.
trim() It trims the white space from the left and right side of
the string.
fromCharCode() The fromCharCode() method converts Unicode
values into characters.
67
68
69
70
71
72
73
Retrieving a position of characters
in a string
To find a character or characters in string we have
two methods, indexOf() and search()
You can determine the index of a character by
calling the indexOf() method of the string object.
The indexOf() method returns the index of the
character passed to it as an argument.
The search() method searches a string for a
specified value and returns the position of the
match.
74
search() method searches a string for a specified
value, and returns the position of the match.
The search value can be string or a regular
expression.
This method returns -1 if no match is found.
Syntax:
string.search(searchvalue);
75
<script>
var str="JavaScript is a scripting language.";
document.writeln(str.search("scripting"));
</script>
Output:
16
76
<html>
<body>
<script>
var s1="JavaScript is a scripting language";
var n=s1.indexOf("a");
document.writeln(n+"<br>");
document.writeln(s1.search("scripting"));
var m=s1.lastIndexOf("a");
document.writeln("<br>"+m);
Output:
</script>
1
</body> 16
</html> 31
77
78
79
80
81
82
83
2. substr()
The substr() extracts the characters from a string,
starting from startindex & of a specified length &
returns the new substring.
Syntax :
string.substr(startindex,[length])
84
85
86
87
1. parseInt()
The parseInt() converts a string into an integer(a whole
number)
It accepts 2 arguments. The first argument is the string
to convert
The second argument is called radix. This is the base
number used in mathematical system. It should always
be 10
Example :
var size= ‘42inch’;
var integer= parseInt(size,10); //return 42
88
2. parseFloat()
The parseFloat() converts a string into a point number
(number with decimal points)
Example :
var size=‘3.14inch’;
var pointNum= parseFloat(size); //returns 3.14
89
3. Number()
It converts a string to a number. If string consist of only
number then you will get expected outcome otherwise it
will return NAN-Not a Number
Example :
Number(‘123’) // returns 123
Number(‘12.3’) // returns 12.3
Number(‘42px’) // returns NaN
Number(‘3.14someRandomStuff’) // returns NaN
90
91
92
93
Changing the Case of the string
JavaScript provides two methods to change the case
of string by using toLowerCase() and toUpperCase()
method.
toLowerCase(): this method converts all the string
character to lowercase.It does not take any argument.
toUpperCase(): this method converts all the string
character to uppercase. It does not take any
argument.
94
<html>
<body>
<script>
var str = "JavaScript";
document.writeln(str.toLowerCase());
document.writeln("<br>"+str.toUpperCase());
</script>
</body>
Output:
</html>
javascript
JAVASCRIPT
95
96
Finding a Unicode of a
character
Unicode is a standard that assigns a number to every
character, number, and symbol that can be displayed on a
computer screen, including characters and symbols that
are used in all languages.
You can determine the Unicode number or the character
that is associated with a Unicode number by using the
charCodeAt() method and fromCharCode() method. Both
are string object methods.
The charCodeAt() method takes an integer as an
argument that represents the index of the character in
which you’re interested. If you don’t pass an argument, it
97 defaults to index 0.
The charCodeAt() method returns the Unicode
number of the string:
var UnicodeNum = StringName.charCodeAt()
If you need to know the character, number, or
symbol that is assigned to a Unicode number, use
the fromCharCode() method.
The fromCharCode() method requires one
argument, which is the Unicode number.
98
charCodeAt() is a string method used to retrieve a Unicode
value for a character at a specific position.
The parameters to the charCodeAt() is index position for
which u want to know the unicode value.
Syntax : string.charCodeAt([position]);
If this position parameter is not provided the charCode() will
use 0 as the default.
99
fromCharCode()
It is used to create a string from a sequence of Unicode
values
The parameters to fromCharCode() method is one or
more unicode values
Syntax:
String.fromCharCode(value1,value2,…..value_n);
10
0
<html>
<body>
<script>
var x="Javatpoint";
document.writeln(x.charCodeAt(3));
document.write("<br>");
var res = String.fromCharCode(72, 69, 76, 76, 79);
var res1 = String.fromCharCode(73, 70, 77, 77, 80);
document.write(res);
document.write("<br>"+res1);
</script>
</body> Output:
</html> 97
HELLO
10 IFMMP
1
Code : charCodeAt()
<script>
var x="Javatpoint";
document.writeln(x.charCodeAt(3));
</script>
Output:
97
10
2
10
3
Thank You
10
4