CH 02 CSS Aug-23
CH 02 CSS Aug-23
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:
Output:
PHP,CSS,Java,Applied Math,Elements of Maths
25
26
27
<html>
<body>
<script> Output:
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.
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:
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
62
63
String Properties
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
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()
Example :
var size= ‘42inch’;
var integer= parseInt(size,10); //return 42
88
2. parseFloat()
Example :
var size=‘3.14inch’;
var pointNum= parseFloat(size); //returns 3.14
89
3. 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
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.
98
charCodeAt() is a string method used to retrieve a Unicode
value for a character at a specific position.
Syntax : string.charCodeAt([position]);
99
fromCharCode()
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