CSS UNIT II
CSS UNIT II
ARRAY
iii) start with zero and end with N – 1 where N is size of array.
iv) In javascript array is a flexible in nature. i.e size of array changes automatically as per
insertion of new values in array. If we delete existing element in array, then array size decrease
automatically.
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<script>
</body>
</html>
Output :
size of array 1 = 0
size of array 2 = 3
size of array 3 = 6
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<script>
arr[0] = 10;
arr[1] = 20;
arr[2] = 30;
</script>
</body>
</html>
Output :
First Element = 10
Second Element = 20
Third Element = 30
<body>
<script>
arr[0] = 10;
arr[1] = "dev";
arr[2] = 34.4;
arr[3] = true;
</script>
Output :
Element 1 = dev
Element 2 = 34.4
Element 3 = true
WAP To implement two dimension array in javascript.
ii) However, we can implement the concept of two dimensional array by creating array of an
array.
<body>
<script>
arr[0][0] = 1;
arr[0][1] = 2;
arr[0][2] = 3;
arr[1][0] = 4;
arr[1][1] = 5;
arr[1][2] = 6;
arr[2][0] = 7;
arr[2][1] = 8;
arr[2][2] = 9;
document.write(arr[i][j]+" ");
</script>
</body>
Output :
123
456
789
i) Sometime it is necessary to add extra element in the existing array of specific size.
1. push( ) method :
i) The push method is used to add one or more elements at the end of the array.
2. unshift( ) method :
i) the unshift( ) method is used to add one or more elements at the beginning of array.
<body>
<script>
arr[0] = 10;
arr[1] = 20;
document.write("<br>");
arr[2] = "direct";
arr.push("push1", "push2");
arr.unshift("unshift");
arr[arr.length] = "lengthproperty";
}
</script>
</body>
Output :
10 20
unshift 10 20 direct push1 push2 lengthproperty
1. pop( ) method
The pop( ) method is used to remove existing element from end of the array.
2. shift( ) method
The shift( ) method is used to remove exiting element from first index of array.
<body>
<script>
arr[0] = 10;
arr[1] = 20;
arr[2] = 30;
document.write("Array elements:<br>")
</script>
</body>
Output :
Array elements:
10 20 30
Array elements after pop :
10 20
Array elements after shift :
20
1. concat( ) method
2. join( ) method
1. concate( ) method
arrayname.concate( );
arrayname.concate(arrayname1,arrayname2…..);
<body>
<script>
let s1 = a.concat();
document.write(s1);
let s2 = a.concat(b);
document.write("<br>" + s2);
</script>
</body>
Output :
1,2,3
1,2,3,4,5,6
2.join( ) method :
i) The join method is also used to combine element of array into string separated by comma(,)
by default.
ii) using joint method, we can use any special symbol to separate elements of array like #, @, -
etc.
<body>
<script>
let s1 = a.join();
document.write(s1);
let s2 = a.join("#");
document.write("<br>" + s2);
</script>
</body>
Output :
1,2,3
1#2#3
Sorting elements of Array :
arrayname.sort();
arrayname.reverse();
arrayname.sort(function(a,b){return a-b});
arrayname.sort(function(a,b){return b-a});
<body>
<script>
document.write(asc);
dsc = b.reverse();
document.write("<br>" + dsc);
//sorting integer array
document.write("<br>" + ascnum);
document.write("<br>", dscnum);
</script>
</body>
Output :
dev,himanshu,prath,vinayak
vinayak,prathmesh,himanshu,dev
0,1,2,3,4,6
10,7,5,4,2
Object as associative array :
i) In order to access the value of property of object, there are two methods.
A. objectname.propertyname
B. objectname[“propertyname”]
ii) for ex. We want to access rno property of student object, there are two ways
A) Student.rno
B) Student[“rno”]
iii) here in the first method, the property name is an identifier and in the second method, the
property name is string
iv) An associative array is a collection of key-value pairs where each key is unique.
v) Instead of using numerical indices to access values (like in a regular array), you use keys,
which are usually strings or symbols.
vi) in order to access the values of associative array special for loop is used.
<body>
<script>
let student = {
name: "devesh",
rno: 06,
city: "Mumbai"
};
for (let key in student) { //here key is name,rno,city(loop will run 3 times as
there are 3 keys)
</script>
</body>
Output :
name = devesh
rno = 6
city = Mumbai
fuction
iii) Syntax :
<script>
Function functionName(<parameters>)
//block of code
</script>
<head>
<script>
function display() {
document.write("Welcome to javascript!");
</script>
</head>
<body>
<script>
display();
</script>
</body>
Output :
Welcome to javascript!
Scope of variable :
i) The scope of a variable in programming refers to the context within which the variable is
accessible or visible.
ii) In JavaScript, the scope of a variable determines where you can use it in your code.
iii) JavaScript has two main types of scope: A) Global scope B) Local scope
Local variable :
Global variable :
<head>
<script>
//global variable
function abc() {
document.write("<br>" + global);
function xyz() {
document.write("<br>" + global);
document.write("<br>" + local);
</script>
</head>
<body>
<script>
abc();
xyz();
</script>
</body>
Output :
global variable
global variable
local variable
Calling a function :
i) syntax : function_name();
i) syntax : function_name(<parameter_list>);
<head>
<script>
function show() {
</script>
</head>
<body>
<script>
show();
</script>
</body>
Output :
<head>
<script>
function add(a, b) {
return a + b;
</script>
</head>
<body>
<script>
</script>
</body>
Output :
sum = 25
ii) The function isn’t called explicitly rather, It will be called in response to an event, such as a
web page is loaded or unloaded by the browser.
iii) For example, if we Have two Different function namely welcome and goodbye. And you
want to code of welcome to be executed when the html web page is loaded in the browser and
goodbye function to be executed when web page is unloaded from the browser.
<head>
<script>
function welcome() {
function goodbye() {
window.alert("Bye.....Bye!");
</script>
</head>
</body>
</html>
Output :
WAP To implement calling function from another function or to implement working of login
page.
<head>
<script>
function login() {
if (valid == true) {
window.alert("Login Successfully!");
else {
window.alert("Invalid Credentials!");
let flag;
flag = true;
else {
flag = false;
return flag;
</script>
</head>
<body onload="login()">
</body>
Output :
Returning value from a function:
<head>
<script>
function area(r) {
let a = 3.14 * r * r;
return a;
</script>
</head>
<body>
<script>
</script>
</body>
Output :
Area = 78.5
Strings :
ii) syntax :
<body>
<script>
let s3 = "karan";
let s4 = 'ritu'
</script>
</body>
Output :
s1 =
s2 = dev
s3 = karan
s4 = ritu
length of s2 = 3
Joining a strings :
The + operator is used to concat if one of the operand is string and returns concatenated string
as a result.
2. concat( ) method :
The concat( ) method is used to join two or more strings and returns the new single string as a
result.
Syntax : stringname1.concat(string2,string3…);
<body>
<script>
let s1 = "dev";
let s2 = "karan";
let s3 = "welcome!"
let s4 = s1 + s2;
document.write("<br>" + s4);
//concat() method
document.write("<br>" + s5);
</script>
</body>
Output :
devkaran
devkaranwelcome!
ii) The charAt( ) function is used to find/access character from particular position.
<body>
<script>
</body>
Output :
character at index 0 : W
character at index 7 :
character at index 15 : s
character at index 50 :
1. indexOF() method
i) This method is used to return the position fo first occurance of specific charactors in a string.
iii) syntax :
stringname.indexOf(“character”);
2. serch( ) method :
i) this method is used to search the desired string and returns the position fo matched string
<body>
<script>
let b = s1.indexOf("?");
let d = s1.search("you");
let e = s1.search("dev");
</script>
</body>
Output :
<body>
<script>
document.write("<br>" + words);
document.write("<br>" + letters);
</script>
</body>
Output :
1. substring( ) method :
i) this method is used to Extract the character from the string between the two specified indices
and return the new string as result.
Here end index is optional and if not written then rest of string considered.
2. substr() method :
i) This method is used to extract characters from the string starting from start_index up to
length specified.
<body>
<script>
</script>
</body>
Output :
Var s1 = 20;
Var s2 = “30”;
ii) to get the desired output we must convert second variable from string to number datatype.
A) parseInt( ) :
i) Used to convert string to Integer(whole number)
ii) It accepts 2 parameter a)string b)radix(base)
B) parseFloat( ) :
i) Used to convert string in floating point number.
C) number( ) method :
i) Used to convert string into number.
ii) These method accepts only number of float values and if the string contains any
character then it will return NAN.
<body>
<script>
let num4 = parseInt(str4); // NaN (cannot parse, starts with a non-numeric character)
let num8 = parseFloat(str8); // NaN (cannot parse, starts with a non-numeric character)
</script>
</body>
Output :
parseInt(str1): 42
parseInt(str2): 42
parseInt(str3): 42
parseInt(str4): NaN
parseFloat(str1): 42.58
parseFloat(str2): 42
parseFloat(str3): 3.14
parseFloat(str4): NaN
Number(str1): 42
Number(str2): 42.58
Number(str3): NaN
Number(str4): NaN
toString( ) method :
<body>
<script>
let a = 10;
let b = 20;
</script>
</body>
Output :
Result = 1020
1. toLowerCase() :
2. toUpperCase( ) :
ii)syntax : stringname.toUpperCase();
WAP to implement changing case of string
<body>
<script>
</script>
</body>
Output :
iii) Hence, when we enter any character, number The corresponding character is automatically
convert into a number equivalent to its unicode.
1.charCodeAt( ) :
i) The charCodeAt() is string method which is used to access the unicode value for perticular
character or a given position.
ii) This method access one parameter as the index value of the desired character.
iii) syntax: stringname.charCodeAt(index);
2.fromCharCode() :
<body>
<script>
//stringname.charCodeAt(index);
//String.fromCharcode(45,67,34)
document.write("<br>");
</script>
</body>
Output :
Unicode of W = 87
Unicode of space = 32
DEVESH