JavaScript
-high level language (now)
-case senstive
-not typesafe
-.js
-initially it is function oriented , now it supports oops
-is executed independently using Node
-no main()
-no explicit datatypes
-it is a collection of objects
-every statement is terminated with semicolon (;), but not mandatory
Price = 99;
console.log('welcome to javascript');
----------
Price = 99;
console.log(Price);
---------------------
Price = 99;
console.log('Price ' + Price);
--------------------------------
Price
console.log(Price);
ReferenceError: Price is not defined
---------------------------------------
Price = 99.29;
console.log(Price + ' ' + typeof(Price));
Price = 399;
console.log(Price + ' ' + typeof(Price));
Price = "JavaScript";
console.log(Price + ' ' + typeof(Price));
--------------------------------------------
Price = 399;
console.log(Price + ' ' + typeof(Price));
Price = "JavaScript";
console.log(Price + ' ' + typeof(Price));
Price = 'ashok it';
console.log(Price + ' ' + typeof(Price));
Price = 'a';
console.log(Price + ' ' + typeof(Price));
Price = 'admin#1234';
console.log(Price + ' ' + typeof(Price));
----------------------------------------
Price = 399;
console.log(Price + ' ' + typeof(Price));
Price = true;
console.log(Price + ' ' + typeof(Price));
Price = false;
console.log(Price + ' ' + typeof(Price));
------------------------------------------
Price = 399;
console.log(Price + ' ' + typeof(Price));
Price = "12/03/2025";
console.log(Price + ' ' + typeof(Price));
--------------------------------------------
Price = 399;
console.log(Price + ' ' + typeof(Price));
Price = "12/03/2025";
console.log(Price + ' ' + typeof(Price));
Price = null;
console.log(Price + ' ' + typeof(Price));
Price = undefined;
console.log(Price + ' ' + typeof(Price));
Price = [];
console.log(Price + ' ' + typeof(Price));
Price = {};
console.log(Price + ' ' + typeof(Price));
--------------------------------------------
Price = 399;
console.log(Price + ' ' + typeof(Price));
function Add(x,y)
{
console.log(x+y);
}
Add(19,29);
Price = Add;
console.log(Price + ' ' + typeof(Price));
Price(10,20);
-----------------------------
datatypes - variables
-no need of datatypes to declare a variable.
-use of variables with out declaring them is possible
-implicit datatypes are available
-datatype of a variable is decided based on the value assigned to it.
-to use a variable, it should be declared or initialized.
-value of a variable is allowed change
-datatype of a variable can be changed
implicit or primitive datatypes
number => when variable is assigned with a numeric value of any type including floating point
string => when variable is assigned with a any type of content enclosed by double quotes or single quotes
boolean => when variable is assigned with true or false
undefined => when a variable is assigned with undefined
object => when variable is assigned with null, [] , {}
function => when a variable is assigned with function name
-----------------------------------------------------------------
DAY - 2
--------
x = Number();
console.log(x);
output : 0
--------------------
x = Number(99);
console.log(x);
-------------------
x = Number(99);
console.log(x + ' ' + typeof(x));
99 number
--------------------------------
x = Number('javascript');
console.log(x + ' ' + typeof(x));
NaN number
-------------------------------
x = Number(true);
console.log(x + ' ' + typeof(x));
1 number
--------------------------------
x = Number(false);
console.log(x + ' ' + typeof(x));
0 number
------------------------------
x = String();
console.log(x + ' ' + typeof(x));
string
-----------------------------
x = String('Ashok It');
console.log(x + ' ' + typeof(x));
Ashok It string
-----------------------------
x = String("Ashok It");
console.log(x + ' ' + typeof(x));
Ashok It string
---------------------------------
x = String(true);
console.log(x + ' ' + typeof(x));
------------------------------
x = String(123456);
console.log(x + ' ' + typeof(x));
--------------------------------
data = 'testing';
x = String(data);
console.log(x + ' ' + typeof(x));
------------------------------------
data = 'testing';
x = String('data');
console.log(x + ' ' + typeof(x));
data string
----------------------------------
x = Boolean();
console.log(x + ' ' + typeof(x));
----------------------------------
x = Boolean(true);
console.log(x + ' ' + typeof(x));
----------------------------------
x = Boolean(false);
console.log(x + ' ' + typeof(x));
-----------------------------------
x = Boolean(1);
console.log(x + ' ' + typeof(x));
----------------------------------
x = BigInt(99);
console.log(x + ' ' + typeof(x));
99 bigint
----------------------------------
x = Object();
console.log(x + ' ' + typeof(x));
---------------------------------
x = Object(99);
console.log(x + ' ' + typeof(x));
---------------------------------
x = Object('Arjun');
console.log(x + ' ' + typeof(x));
----------------------------------
x = Object(true);
console.log(x + ' ' + typeof(x));
---------------------------------
x = Object(null);
console.log(x + ' ' + typeof(x));
----------------------------------
x = Object(undefined);
console.log(x + ' ' + typeof(x));
---------------------------------
x = Date();
console.log(x + ' ' + typeof(x));
---------------------------------
Number()
String()
BigInt()
Boolean()
Object()
Date()
---------------------------------
declarion of variables
var let
var Price = 99;
console.log(Price + ' ' + typeof(Price));
-----------------------------------------
let Price = 99;
console.log(Price + ' ' + typeof(Price));
-----------------------------------------
var Price
console.log(Price + ' ' + typeof(Price));
undefined undefined
Note: a variable without value becomes undefined
----------------------------------------
Operators :
Arithmetic Operators
+ - * / % *
var x = 10;
var y = 2;
var z = 3;
console.log(x + y);
console.log(x - y);
console.log(x * y);
console.log(x / y);
console.log(x % y);
console.log(x % z);
console.log(x ** y);
--------------------------------
var x = true;
var y = false;
console.log(x + y);
console.log(x - y);
console.log(x * y);
console.log(x / y);
------------------------------
var x = 10;
var y = 3;
console.log(x + y);
console.log(x - y);
console.log(x * y);
console.log(x / y);
13
7
30
3.3333333333333335
-------------------------
sign operators
+
-
var x = +10;
var y = -6;
console.log(x + ' ' + y);
------------------------------
x = 10
y = 2
z = x + y;
x + y ---> arithmetic expression
x y ----> operands
+ ------> operator
----------------------------
increment and decrement
++
--
var x = 10;
var y = x;
console.log(x + ' ' + y);
-------------------------------
var x = 10;
var y = ++x; //(pre) increment first - assign later
console.log(x + ' ' + y);
----------------------------------
var x = 10;
var y = --x; //(pre) decrement first - assign later
console.log(x + ' ' + y);
----------------------------------
var x = 10;
var y = x--; //(post) assign first - decrement later
console.log(x + ' ' + y);
----------------------------------
var x = 10;
var y = x++; //(post) assign first - increment later
console.log(x + ' ' + y);
----------------------------------
short-cut operators
+=
-=
*=
/=
%=
var x = 10;
x = x + 2;
// x = 10 + 2;
// x = 12;
console.log(x);
-----------------------------
var x = 10;
//x = x + 2;
x += 2;
console.log(x);
-----------------------------
var x = 10;
//x = x - 2;
x -= 2;
console.log(x);
--------------------------
var x = 10;
//x = x * 2;
x *= 2;
console.log(x);
-------------------------
var x = 10;
//x = x / 2;
x /= 2;
console.log(x);
-------------------------
var x = 10;
//x = x % 2;
x %= 2;
console.log(x);
-------------------------
relative operators or comparison operators
>
<
>=
<=
==
===
!=
Note: any valid relative expression always returns boolean value. so these are called boolean expressions.
var x = 10;
var y = 2;
var z = 10;
console.log(x > y); // 10 > 2--> true
console.log(x > z); // 10 > 10 --> false
console.log(x>=z); // 10 >= 10 --> true
console.log(x < y); // 10 < 2 ---> false
console.log(x < z); // 10 < 10 --> false
console.log(x <= z); // 10 <= 10 ---> true
console.log(x == y); // 10 == 2 ---> false
console.log(x == z); // 10 == 10 ---> true
console.log(x != y); // 10 != 2 ---> true
console.log(x != z); // 10 != 10 ---> false
-----------------------------------------------
var x = 10;
var y = 10;
var z = '10';
var a = '99';
var b = 'arjun';
console.log(x + ' ' + y + ' '+ typeof(x) + ' ' + typeof(y) + ' ' + (x == y));
console.log(x + ' ' + z + ' '+ typeof(x) + ' ' + typeof(z) + ' ' + (x == z));
console.log(x + ' ' + a + ' '+ typeof(x) + ' ' + typeof(a) + ' ' + (x == a));
console.log(x + ' ' + b + ' '+ typeof(x) + ' ' + typeof(b) + ' ' + (x == b));
console.log(x + ' ' + z + ' '+ typeof(x) + ' ' + typeof(z) + ' ' + (x === z));
-----------------------------------------------------------------------------------
logical operators
&&
||
!
&& and
|| or
-used to join multiple conditions together as one unit
&&
-returns true, when all the joined condition's result is true
-cond1 && cond2 && cond3 && ......&& condN
-if any one condition's result is false, no need to evaluate the remaining conditions
|| or
- returns true, when atleast one condition's result is true
-cond1 || cond2 || cond3 || ......|| condN
-if any one condition's result is true, no need to evaluate the remaining conditions
! - returns opposite of the condition's result
console.log(true && true);
console.log(true && false);
console.log(false && true);
console.log(false && false);
true
false
false
false
------------------------------
console.log(true || true);
console.log(true || false);
console.log(false || true);
console.log(false || false);
true
true
true
false
-------------------------------
console.log(true );
console.log(false);
console.log(!true);
console.log(!false);
true
false
false
true
------------------------------
console.log(!1);
console.log(!0);
------------------------------
ternary operator
? :
-----------------------------
control structures :
-used to control the flow of execution among the statements of the program.
-selection of one or more statements either to execute or to skip from execution based on a condition.
console.log('Good Monring');
console.log('Good AfterNoon');
console.log('Good Evening');
cs are two types
-non-iterative cs
-iterative cs
-non-iterative cs
1.if
a.simple if - only one option
if(condition)
{
// executes when the condition's result is true
// some logic
}
b.if...else - two options
if(condition)
{
// executes when the condition's result is true
// some logic
}
else
{
// executes when the condition's result is false
// some logic
}
c.if..else if...else..if...else (ladder if)
if(condition1)
{
// executes when the condition's result is true
// some logic
}
else if(condition2)
{
// executes when the condition's result is true
// some logic
}
else if(condition3)
{
// executes when the condition's result is true
// some logic
}
-----------------------
-----------------------
-----------------------
[else
{
// executes when all the condition's results are false
// logic
}]
4.switch - used to check one value against multiple options
switch(variable/exp/value)
{
case value1:
// logic
break;
case value2:
// logic
break;
case value3:
// logic
break;
---------------
---------------
---------------
---------------
default:
// executes when no case value is matched
break;
}
-------------------------------------
x = 10;
y = 10;
if(x < y)
{
console.log(x + ' is less than ' + y);
}
else if(x > y)
{
console.log(x + ' is greater than ' + y);
}
else
{
console.log(x + ' is equal to ' + y);
}
--------------------------------------------
x = 10;
y = 10;
if(x < y)
{
console.log(x + ' is less than ' + y);
}
else if(x > y)
{
console.log(x + ' is greater than ' + y);
}
else if(x == y)
{
console.log(x + ' is equal to ' + y);
}
--------------------------------------------
userType ='visitor'; // use different values
switch(userType)
{
case 'admin': // if(userType == 'admin')
console.log('load admin page');
break;
case 'employee':
console.log('load employee page');
break;
case 'customer':
console.log('load customer page');
break;
case 'supplier':
console.log('load supplier page');
break;
case 'guest':
console.log('load guest page');
break;
default:
console.log('invalid user type...check once');
break;
}
-----------------------------------------------------------
x = 35;
switch(true) // true
{
case x>=90: // true == false
console.log('A+');
break;
case x>=80: // true == false
console.log('A');
break;
case x>=70: // true == false
console.log('B');
break;
case x>=60: // true == false
console.log('C');
break;
case x>=50: // true == false
console.log('D');
break;
case x>=35: // true == true
console.log('E');
break;
default:
console.log('F');
break;
}
-------------------------------------------
iterative control structures
a.while
b.do..while
c.for
-used to execute one or more statements again and again based on a condition
-avoids duplication of code
-supports re-usability
-write once - use many times.
declaration and initialization of looping variable
condition on looping variable's value
change in the looping variable's value
---------------
var i = 1;
while(i<=6) // condition checking is at the starting /entry point
{
console.log(i + " inside the while loop " + (i<=6))
i = i +1;
}
console.log(i + " outside the while loop " + (i<=6))
1 inside the while loop true
2 inside the while loop true
3 inside the while loop true
4 inside the while loop true
5 inside the while loop true
6 inside the while loop true
7 outside the while loop false
-----------------------------------
var i = 1;
do
{
console.log(i + " inside the do-while loop " + (i<=6));
i = i +1;
}while(i<=6)
console.log(i + " outside the do-while loop " + (i<=6));
------------------------------------------------------------
var i = 11;
do
{
console.log(i + " inside the do-while loop " + (i<=6));
i = i +1;
}while(i<=6)
console.log(i + " outside the do-while loop " + (i<=6));
11 inside the do-while loop false
12 outside the do-while loop false
Note: do-while executes at-least once even the condition's result is false.
-------------------------------------------
var i = 11;
while(i<=6) // condition checking is at the starting /entry point
{
console.log(i + " inside the while loop " + (i<=6))
i = i +1;
}
console.log(i + " outside the while loop " + (i<=6))
11 outside the while loop false
--------------------------------------------------
for
---
var i;
for(i=1;i<=6;i = i +1)
{
console.log(i + " inside the for loop " + (i<=6))
}
console.log(i + " outside the for loop " + (i<=6));
1 inside the for loop true
2 inside the for loop true
3 inside the for loop true
4 inside the for loop true
5 inside the for loop true
6 inside the for loop true
7 outside the for loop false
------------------------------------
var i;
for(i=11;i<=6;i = i +1)
{
console.log(i + " inside the for loop " + (i<=6))
}
console.log(i + " outside the for loop " + (i<=6));
11 outside the for loop false
-------------------------------------
var i=1;
for(;i<=6;)
{
console.log(i + " inside the for loop " + (i<=6))
i = i +1;
}
console.log(i + " outside the for loop " + (i<=6));
-------------------------------------------------------
var i=1;
for(;;)
{
console.log(i + " inside the for loop " + (i<=6))
i = i +1;
}
// unreachable code
console.log(i + " outside the for loop " + (i<=6));
//infinite loop
-----------------------------------
var i=1;
for(;;)
{
console.log(i + " inside the for loop " + (i<=6))
if(i==6)
{
break;
}
i = i +1;
}
console.log(i + " outside the for loop " + (i<=6));
1 inside the for loop true
2 inside the for loop true
3 inside the for loop true
4 inside the for loop true
5 inside the for loop true
6 inside the for loop true
6 outside the for loop true
--------------------------------------------
array ->
- collection of values
- *may be of same type or different type or other arrays
- stored in a conitinious memory location
- indentified by their position called index.
- index value starts with zero '0' and ends with array size minus one.
example : array with 9 values
index starts with 0 and ends with 8.
- index can be string, but it is not reflects the size.
- all values shares same name.
- array in javascript created in two ways
-using []
-using Array()
- var arrayname = [val1,val2,val3,......valN];
- to read a value
arrayname[index]
- to assign a value
arrayname[index] = value;
array management:
-creation of array
-array values management
-add
-delete
-edit/modify/update
-search/find
-sort
-other
-array methods
----------------------
var marks = [99,35,45,88,58,69];
console.log(marks[0]);
console.log(marks[1]);
console.log(marks[2]);
console.log(marks[3]);
console.log(marks[4]);
console.log(marks[5]);
99
35
45
88
58
69
---------------------------
var marks = [99,35,45,88,58,69];
console.log(marks[0]);
marks[0] = 89;
console.log(marks[0]);
------------------------------
var marks = [99,35,45,88,58,69];
for(i in marks)
{
console.log(i + ' value ' + marks[i]);
}
0 value 99
1 value 35
2 value 45
3 value 88
4 value 58
5 value 69
---------------------------------------------
var marks = [99,35,45];
for(i in marks)
{
console.log(i + ' value ' + marks[i]);
}
0 value 99
1 value 35
2 value 45
----------------------------
var marks = [99,35,45,66];
for(i in marks)
{
console.log(i + ' value ' + marks[i]);
}
0 value 99
1 value 35
2 value 45
3 value 66
--------------------------------
var marks = [];
for(i in marks)
{
console.log(i + ' value ' + marks[i]);
}
------------------------
Creation of an array:
using []
using Array()
----------------------------------
var marks = []; // empty array
console.log(typeof(marks)); // object
console.log('size of marks : ' + marks.length);
//size of marks : 0
console.log(marks); //[]
-----------------------------------
var marks = [88,44,99,55,46,39]; // with values - same type
console.log(typeof(marks)); // object
console.log('size of marks : ' + marks.length);
//size of marks : 6
console.log(marks); //[ 88, 44, 99, 55, 46, 39 ]
---------------------------------------------------------
var Product = [9009,'Apple','12pc',199,true];
// with values - different type
console.log(typeof(Product)); // object
console.log('size of Product : ' + Product.length);
//size of Product : 5
console.log(Product);
//[ 9009, 'Apple', '12pc', 199, true ]
-----------------------------------------
var Student = [1009,'John Miller','MERN','17/03/2025','Adilabad',18000.00,true];
console.log(Student);
console.log('size : ' + Student.length);
------------------------------------------
var FoodItem =
[
2009,
'Butter Masala Dosa',
125,
4.1,
2194,
'Best Seller',
'Veg',
true,
'Butter-Masala-Dosa.jpg'
]
console.log(FoodItem);
console.log('size of FoodItem : ' + FoodItem.length);
---------------------------------------------------------
https://www.bigbasket.com/media/uploads/p/m/40072494_12-bb-royal-organic-turtoor-dal.jpg?tr=w-154,q-80
-----------------------------------------------------------------------------------------------------
var Product =
[
9001,
'Organic Toor/Arhar Dal',
'bb Royal',
'4',
'20245 Ratings',
'Veg',
'41% OFF',
'500g',
'1 kg',
'2 kg',
'29%',
'41%',
'33%',
150,
299,
589,
105.93,
177.21,
393.03,
true,
true,
true,
'40072494_12-bb-royal-organic-turtoor-dal.jpg',
true
];
console.log(Product);
console.log('size of Product: ' + Product.length);
------------------------------------------------------
Product
-ProductId - only one value
-Name - only one value
-BrandName - only one value
-TypeOfProduct -only one value
-Rating - only one value
-RatingBy - only one value
-Quantity - multi valued property
-Mrp - multi valued property
-Discount - multi valued property
-SalePrice - multi valued property
-IsAvailable - multi valued property
-ImagePath - only one value
-Status - only one value
Number of properties : 13
Number of values : 23
Note: to handle 23 values by using 13 properties
--------------------------------------------
var Quantity = ['500g','1 kg','2 kg'];
var Mrp = [150,299,589];
var Discount = ['29%','41%','33%'];
var SalePrice = [105.93,177.21,393.03]
var QtStatus = [true,true,true]
var Product =
[
9001,
'Organic Toor/Arhar Dal',
'bb Royal',
'4',
'20245 Ratings',
'Veg',
'40072494_12-bb-royal-organic-turtoor-dal.jpg',
true,
Quantity,
Mrp,
Discount,
SalePrice,
QtStatus
];
console.log(Product);
console.log('size of Product: ' + Product.length);
------------------------------------------------------------------
var Quantity = ['500g','1 kg','2 kg'];
var Mrp = [150,299,589];
var Discount = ['29%','41%','33%'];
var SalePrice = [105.93,177.21,393.03]
var QtStatus = [true,true,true]
var Product =
[
9001,
'Organic Toor/Arhar Dal',
'bb Royal',
'4',
'20245 Ratings',
'Veg',
'40072494_12-bb-royal-organic-turtoor-dal.jpg',
true,
Quantity,
Mrp,
Discount,
SalePrice,
QtStatus
];
console.log(Product);
console.log('size of Product: ' + Product.length);
console.log('Name :' + Product[1]);
console.log('Brand :' + Product[2]);
console.log("Rating :" + Product[3]);
console.log("Rating Given By : " + Product[4]);
console.log('Type of Product : ' + Product[5]);
console.log('Image path : ' + Product[6]);
console.log('Product is available : ' + Product[7] );
console.log("Quantity : " + Product[8])
console.log('1st Quantity : ' + Product[8][0] )
console.log('1st Mrp : ' + Product[9][0] )
console.log('1st Discount : ' + Product[10][0] )
console.log('1st SalePrice : ' + Product[11][0] )
------------------------------------------------------
creating an array by using Array()
var marks = Array(); // empty array
console.log(marks);
console.log(marks.length);
-------------------------------
var marks = Array(9); // array with single value
console.log(marks); // creates array with the given size
console.log(marks.length);
-------------------------------------------
var marks = Array(99,44,77,88); // array with multiple values
console.log(marks);
console.log(marks.length);
----------------------------
var Quantity = Array('500g','1 kg','2 kg');
var Mrp = Array(150,299,589);
var Discount = Array('29%','41%','33%');
var SalePrice = Array(105.93,177.21,393.03);
var QtStatus = Array(true,true,true);
var Product =
[
9001,
'Organic Toor/Arhar Dal',
'bb Royal',
'4',
'20245 Ratings',
'Veg',
'40072494_12-bb-royal-organic-turtoor-dal.jpg',
true,
Quantity,
Mrp,
Discount,
SalePrice,
QtStatus
];
console.log(Product);
console.log('size of Product: ' + Product.length);
console.log('Name :' + Product[1]);
console.log('Brand :' + Product[2]);
console.log("Rating :" + Product[3]);
console.log("Rating Given By : " + Product[4]);
console.log('Type of Product : ' + Product[5]);
console.log('Image path : ' + Product[6]);
console.log('Product is available : ' + Product[7] );
console.log("Quantity : " + Product[8])
console.log('1st Quantity : ' + Product[8][0] )
console.log('1st Mrp : ' + Product[9][0] )
console.log('1st Discount : ' + Product[10][0] )
console.log('1st SalePrice : ' + Product[11][0] )
---------------------------------------------------------
Read a value
arrayname[index]
Assign a value
arrayname[index]=value;
--------------------------------
var marks = [99,44,77,88,11,89,23,95,59];
console.log(marks);
console.log(marks.length);
console.log(marks[0]); // valid index values 0 to 8
console.log(marks[35]); // undefined
Note: value from a non-existing index becomes undefined
---------------------------------------------------------
var marks = [99,44,77,88,11,89,23,95,59];
console.log(marks);
console.log(marks.length);
console.log(marks[0]); // valid index values 0 to 8
marks[0] = 69;
console.log(marks[0]);
console.log(marks);
marks[12] = 8;
console.log('size : ' + marks.length);
console.log(marks);
//Note: when value assinged to a non-existing index
//memory is allocated with the given index.
--------------------------------------------------
var Product = []; // empty
console.log(Product);
console.log('size : ' + Product.length);
Product[0] = 9009;
console.log('size : ' + Product.length);
Product[1] = 'Apple';
console.log('size : ' + Product.length);
Product[2] = '1Kg';
console.log('size : ' + Product.length);
Product[3] = 199.99;
console.log('size : ' + Product.length);
console.log(Product);
---------------------------------------
var Product = [];
console.log(Product);
Product['ProductId'] = 9009;
Product['ProductName'] = 'Green Mango';
Product['Quantity'] = '1Kg';
Product['Price'] = 99;
console.log(Product);
console.log('size : ' + Product.length); // 0
console.log(Product['ProductId']);
console.log(Product['ProductName']);
console.log(Product['Quantity']);
console.log(Product['Price']);
output
[]
[
ProductId: 9009,
ProductName: 'Green Mango',
Quantity: '1Kg',
Price: 99
]
size : 0
9009
Green Mango
1Kg
99
-------------------------------
Array methods
-add and remove
-push() : used to append the given value/values/array at the end of the array and
it returns the new size of the array.
-unshift() - used to insert the value at the start of an array and returns the new size
-pop() - used to remove the value from end of an array.
It returns the removed value.
-shift() - used to remove and return the value from the top/starting of an array.
-splice() - used to insert/remove/replace
-includes() - used to search for a given element.
-indexOf() - used to search and to find the index of a given element
-lastIndexOf() - used to search and to find the index of a given element from left side of an array.
- findIndex()
- map()
- filter()
var marks = [99,44,77,88,11,89,23,95,59];
console.log('size ' + marks.length);
console.log(marks.push(49)); // with single value
console.log(marks);
--------------------------------------------
var marks = [99,44,77,88,11,89,23,95,59];
console.log('size ' + marks.length);
console.log(marks.push(49,19,29)); // with multiple value
console.log(marks);
--------------------------------------------
var marks = [99,44,77,88,11,89,23,95,59];
console.log('size ' + marks.length);
console.log(marks.push([49,19,29])); // with array
console.log(marks);
--------------------------------------------
var marks = [99,44,77,88,11,89,23,95,59];
console.log('size ' + marks.length);
console.log(marks.unshift(49)); // with single value
console.log(marks);
------------------------------------
var marks = [99,44,77,88,11,89,23,95,59];
console.log('size ' + marks.length);
console.log(marks.unshift(49,19,29)); // with multiple value
console.log(marks);
-------------------------------------
var marks = [99,44,77,88,11,89,23,95,59];
console.log('size ' + marks.length);
console.log(marks.unshift([49,19,29])); // with array
console.log(marks);
--------------------------------------
var marks = [99,44,77,88,11,89,23,95,59];
console.log('size ' + marks.length);
console.log(marks.pop()); // removes and returns the end value
console.log(marks);
console.log('size ' + marks.length);
-------------------------------------
var marks = [99,44,77,88,11,89,23,95,59,[19,29,39]];
console.log('size ' + marks.length);
console.log(marks.pop()); // removes and returns the end value
// here the last value is array
console.log(marks);
console.log('size ' + marks.length);
----------------------------------------------------
var marks = [99,44,77,88,11,89,23,95,59];
console.log('size ' + marks.length);
console.log(marks.shift()); // removes and returns the start value
console.log(marks);
console.log('size ' + marks.length);
-----------------------------------------
var marks = [[12,23,29],99,44,77,88,11,89,23,95,59];
console.log('size ' + marks.length);
console.log(marks.shift()); // removes and returns the start value
// which is an array
console.log(marks);
console.log('size ' + marks.length);
------------------------------------------
var marks = [23,95,59];
console.log('size ' + marks.length);
console.log(marks.pop());
console.log('size ' + marks.length);
console.log(marks.pop());
console.log('size ' + marks.length);
console.log(marks.pop());
console.log('size ' + marks.length);
console.log(marks);
console.log(marks.pop()); // undefined
--------------------------------------
var marks = [23,95,59];
console.log('size ' + marks.length);
console.log(marks.shift());
console.log('size ' + marks.length);
console.log(marks.shift());
console.log('size ' + marks.length);
console.log(marks.shift());
console.log('size ' + marks.length);
console.log(marks);
console.log(marks.shift()); // undefined
Note: pop()/shift() on empty array returns undefined.
---------------------------------------------
var marks = [99,44,77,88,11,89,23,95,59];
// to add
// 19 is inserted at 0th index
// no values to delete
var result = marks.splice(0,0,19);
console.log(result);
console.log(marks);
[]
[
19, 99, 44, 77, 88,
11, 89, 23, 95, 59
]
---------------------------
var marks = [99,44,77,88,11,89,23,95,59];
//19 is inserted at 2nd index
var result = marks.splice(2,0,19);
console.log(result);
console.log(marks);
[]
[
99, 44, 19, 77, 88,
11, 89, 23, 95, 59
]
------------------------------
var marks = [99,44,77,88,11,89,23,95,59];
// delete 1 value from 2nd index
var result = marks.splice(2,1);
console.log(result);
console.log(marks);
[ 77 ]
[
99, 44, 88, 11,
89, 23, 95, 59
]
----------------------------
var marks = [99,44,77,88,11,89,23,95,59];
// delete 3 values from 2nd index
var result = marks.splice(2,3);
console.log(result);
console.log(marks);
[ 77, 88, 11 ]
[ 99, 44, 89, 23, 95, 59 ]
---------------------------------------
var marks = [99,44,77,88,11,89,23,95,59];
// replace = delete and insert at same index
var result = marks.splice(2,1,29);
console.log(result);
console.log(marks);
[ 77 ]
[
99, 44, 29, 88, 11,
89, 23, 95, 59
]
------------------------
var marks = [99,44,77,88,11,89,23,95,59];
// insertion of multiple values at 2nd index
var result = marks.splice(2,0,1,22,333,444);
console.log(result);
console.log(marks);
[]
[
99, 44, 1, 22, 333, 444,
77, 88, 11, 89, 23, 95,
59
]
-----------------------------------
var marks = [99,44,77,88,11,89,23,95,59];
// insertion of array at 2nd index
var result = marks.splice(2,0,[1,22,333,444]);
console.log(result);
console.log(marks);
[]
[ 99, 44, [ 1, 22, 333, 444 ], 77, 88, 11, 89, 23, 95, 59 ]
-------------------------------------------------------------
var marks = [99,44,77,88,11,89,23,95,59];
// insertion of array at 2nd index (using array name)
var v = [1,22,333,444];
var result = marks.splice(2,0,v);
console.log(result);
console.log(marks);
[]
[ 99, 44, [ 1, 22, 333, 444 ], 77, 88, 11, 89, 23, 95, 59 ]
-----------------------------------------------------
var marks = [99,44,77,88,11,89,23,95,59];
console.log(marks.includes(29));
false
--------------------------------------
var marks = [99,44,77,88,11,89,23,95,59];
console.log(marks.includes(11));
true
---------------------------------------
var marks = [99,44,11,77,88,11,89,23,95,11,59,999,0,69];
// searches for the given value from the given index (including index)
console.log(marks.includes(11)); // 1st
console.log(marks.includes(11,3));
console.log(marks.includes(11,6));
console.log(marks.includes(11,10));
---------------------------------------
var marks = [99,44,11,77,88,11,89,23,95,11,59,999,0,69];
// searches for the given value either from 0th index
// or from the given index. if found returns it's position
// if not found return -1
console.log(marks.indexOf(29));
console.log(marks.indexOf(88));
---------------------------------------
var marks = [99,44,11,77,88,11,89,23,95,11,59,999,0,69];
// searches for the given value either from 0th index
// or from the given index. if found returns it's position
// if not found return -1
console.log(marks.indexOf(11)); // 1st 11 - 2nd index
console.log(marks.indexOf(11,3)); // 2nd 11 - 5th index
console.log(marks.indexOf(11,6)); // 3rd 11 - 9th index
console.log(marks.indexOf(11,10)); // 4th 11 - -1
--------------------------------------------------------
var marks = [99,11,44,77,88,89,11,23,95,59,11,999,0,69];
// searches for the given value either from 0th index
// or from the given index. if found returns it's position
// if not found return -1
console.log(marks.indexOf(11)); // 1st 11
console.log(marks.indexOf(11,marks.indexOf(11)+1)); // 2nd 11
console.log(marks.indexOf(11,marks.indexOf(11,marks.indexOf(11)+1)+1)); // 3rd 11
console.log(marks.indexOf(11,marks.indexOf(11,marks.indexOf(11,marks.indexOf(11)+1)+1)+1)); // 4th 11 - -1
-----------------------------------------------------------------------------------------------------------------
var marks = [99,11,44,77,88,89,11,23,95,59,11,999,0,69];
console.log(marks.length);
console.log(marks.lastIndexOf(11));
console.log(marks.lastIndexOf(11,9));
console.log(marks.lastIndexOf(11,5));
console.log(marks.lastIndexOf(11,0));
------------------------------------------------
function -
- is a collection of one or more statements indentified by a name
- write once - use many times in many places.
- re-usability
function Add(x,y) // function definition x y are input parameters
{
console.log(x+y);
}
Add(10,20); // calling statement - 10 20 are called as arguments
// x = 10 y = 20
-----------------------------------
function Add(x,y) // 2 input parameters
{
console.log(x+y);
}
Add(100,200,300,400); // 4 arguments - extra arguments overflow
----------------------------------------------------------------
function Add(x,y) // 2 input parameters
{ // x = 100
// y = ?
// a variable without value becomes undefined
// 100 + undefined
console.log(x+y);
}
Add(100); // 1 arguments
------------------------------------
function are created in three ways
a.named function
b.un-named function
c.arrow function
a function returns a value :
function Add(x, y)
{
z = x + y;
return z;
}
var sum = 0;
sum = Add(10,20);
console.log('sum = ' + sum);
--------------------------------------------
assigning un-named function to a variable as a value
Note: a function without name is called un-named function
var Sum = function (x, y)
{
console.log(x+y);
}
Sum(10,20);
------------------------------------
var Sum = function (x, y)
{
console.log(x+y);
}
Sum(10,20);
console.log(Sum);
console.log(Sum +'');
----------------------------
AddProduct()
CreateProduct()
NewProduct()
SaveProduct()
-----------------------
var Sum = function (x, y)
{
console.log('Sum of x and y ' + (x+y));
}
Sum(10,20);
var Total = Sum;
Total(100,200);
var Add = Total;
Add(1000,2000);
Note: all variables points to only one copy of function
----------------------------------------------------------
var Sum = function Add(x, y)
{
console.log('Sum of x and y ' + (x+y));
}
Sum(10,20);
Add(100,200); // ReferenceError: Add is not defined
---------------------------------------------------
function Print(x, y)
{
console.log(x+y);
}
Print(19,29); // with numbers
Print("java","script"); // with strings
Print(123,"telugu.com"); // with number string
Print(true,true); // with boolean
Print(299.12,399.33); // with floating point numbers
Print('Today :',Date()); // with string , output of Date()
Print(null,null); // with null
Print(undefined,undefined); // with undefined
------------------------------------------------
Note: a function can be passed as an argument to other function
function f1()
{
console.log(' i am function f1 ');
}
function f2()
{
console.log(' i am function f2 ');
}
function f3(x, y)
{
console.log(' i am f3');
x();
y();
}
f3(f1,f2); // x = f1 y = f2
Note:
f1, and f2 are executed as a part of the function f3
Note : a function which is passed as an argument to a function is called callback function
------------------------------------------------------------------------------------------
HOF : higher order function
---------------------------
- a function which takes other function as input (argument)
or
- a function which returns a function
- is called higher order function
In the above example f3 is HOF because it is taking f1 and f2 as input (arguments)
f1 and f2 are called as callback functions.
----------------------------------------------------------------------
function f1()
{
console.log(' i am function f1 ');
function f2()
{
console.log(' i am function f2 created inside function f1');
}
return f2;
}
var f3 = f1();
f3();
-----------------------------------------------------
variables - scope - availability
var x = 99;
console.log('start outside x:' + x);
function f1()
{
console.log(' inside f1 x :' + x);
x = 199;
}
function f2()
{
console.log(' inside f2 x : ' + x);
}
f1();
f2();
console.log('end outside x :'+ x);
--------------------------------------------------------
local variables using var
--------------------------
var x = 99; // available in the entire index.js
console.log('start outside x:' + x);
function f1()
{
var x = 299; // local to f1 only
console.log(' inside f1 x :' + x);
}
function f2()
{
var x = 399; // local to f2 only
console.log(' inside f2 x : ' + x);
}
f1();
f2();
console.log('end outside x :'+ x);
start outside x:99
inside f1 x :299
inside f2 x : 399
end outside x :99
----------------------------------
local var variable is not available to outside
------
function f1()
{
var x = 299; // local to f1 only
console.log(' inside f1 x :' + x);
}
f1();
console.log('end outside x :'+ x);
//ReferenceError: x is not defined
------------------------------------------
var x = 99;
var x = 199; // 2nd x uses 1st x memory
console.log('start x : ' + x);
function f1()
{
var x = 299; // local to f1 only
console.log(' inside f1 x :' + x);
}
f1();
console.log('end outside x :'+ x);
start x : 199
inside f1 x :299
end outside x :199
Note: when more than one variable is created with same name in a scope(program/function) using var ,
all variables which are with same name points same memory.
--------------------------------------------------------------------------
function f1()
{
var x = 299; // local to f1 only
var x = 399;
console.log(' inside f1 x :' + x);
}
f1();
----------------------------------------
now declare the variables using let instead of var
--------------------------------------------------
let x = 99;
console.log('start x using let ' + x);
function f1()
{
console.log('inside f1 x ' + x);
x = 199;
}
f1();
console.log(' outside x using let : ' + x);
-------------------------------------------------
Note: local variables using let
--------------------------------
let x = 99;
console.log('start x using let ' + x);
function f1()
{
let x = 299;
// local to f1 (created insde f1 and available to f1 only)
console.log('inside f1 x ' + x);
}
f1();
console.log(' outside x using let : ' + x);
----------------------------------------------
Note: re-declation of one more variable uisng let with same name in a scope is invalid or error
let x = 99;
console.log('start x using let ' + x);
//let x = 399; // error
function f1()
{
let x = 299;
//let x = 399; // error
// local to f1 (created insde f1 and available to f1 only)
console.log('inside f1 x ' + x);
}
f1();
console.log(' outside x using let : ' + x);
-------------------------------------------------
const
-----
- used to create const fields (variables)
- must initialized along with declaration.
- not allowed to change the value
- re-declaration more than one const with name is not allowed
- can be applied on object (abstract/reference) type also.
const Price;
console.log(Price);
SyntaxError: Missing initializer in const declaration
-----------------------------------------------------
const Price = 299;
console.log(Price);
-----------------------------------------------------
const Price = 299;
console.log(Price);
Price = 399;
console.log(Price);
TypeError: Assignment to constant variable.
-----------------------------------------------------
const x = 10;
const x = 20;
error
-----------------------------
const x = 10;
const y = 20;
const z = x + y;
console.log(x + ' ' + y + ' ' + ' ' + z);
-----------------------------------------------
const x = 10;
const y = 20;
var i = 40;
const z = x + y + 30 + i;
console.log(x + ' ' + y + ' ' + ' ' + z);
-----------------------------------------------
const Status = true;
console.log(Status);
----------------------------------------------
const Location = 'AshokIT';
console.log(Location);
---------------------------------------
Note
const marks = [99,39,49,88,45,96];
console.log(marks);
console.log(marks[0]);
marks[0] = 100;
console.log(marks[0]);
console.log(typeof(marks));
------------------------------------
array methods - 2
-----------------
var Names = ['java','react','angular','csharp','python','c++',
'go','vb','f#','c'];
Names.sort();
console.log(Names);
[
'angular', 'c',
'c++', 'csharp',
'f#', 'go',
'java', 'python',
'react', 'vb'
]
-----------------------------------
var marks = [9,9,46,0,56,23,-3,6,35,78,5,68,2,89,11];
function comp(x,y)
{
return (x-y);
}
marks.sort(comp);
console.log(marks);
[
-3, 0, 2, 5, 6, 9,
11, 12, 23, 35, 46, 56,
68, 78, 89
]
------------------------------------
var marks = [9,12,46,0,56,23,-3,6,35,78,5,68,2,89,11];
function comp(x,y)
{
//return (x-y);
return(y-x);
}
marks.sort(comp);
console.log(marks);
[
89, 78, 68, 56, 46, 35,
23, 12, 11, 9, 6, 5,
2, 0, -3
]
--------------------------------
var marks = [9,12,46,0,56,23,-3,6,35,78,5,68,2,89,11];
marks.sort(function comp(x,y)
{
return (x-y);
});
console.log(marks);
[
-3, 0, 2, 5, 6, 9,
11, 12, 23, 35, 46, 56,
68, 78, 89
]
----------------------------------------
var marks = [9,12,46,0,56,23,-3,6,35,78,5,68,2,89,11];
marks.sort(function (x,y)
{
return (x-y);
});
console.log(marks);
[
-3, 0, 2, 5, 6, 9,
11, 12, 23, 35, 46, 56,
68, 78, 89
]
--------------------------------------------------
// arrow functions
var Add =(x,y)=>
{
console.log(x+y)
};
Add(10,20);
-----------------------------------------
var marks = [9,12,46,0,56,23,-3,6,35,78,5,68,2,89,11];
marks.sort((x,y)=>
{
return (x-y);
});
console.log(marks);
[
-3, 0, 2, 5, 6, 9,
11, 12, 23, 35, 46, 56,
68, 78, 89
]
-----------------------------------------
var marks = [9,12,46,0,56,23,-3,6,35,78,5,68,2,89,11];
marks.filter(f1)
function f1()
{
}
f1(9)
f1(12)
f1(46)
f1(0)
-----
f1(11)
--------------------------------
var marks = [9,12,46,0,56,23,-3,6,35,78,5,68,2,89,11];
function check(x)
{
return (x>=35);
}
var result = marks.filter(check)
console.log(result);
[ 46, 56, 35, 78, 68, 89 ]
//check(9) false
//check(12) false
//check(46) true
---------------------------------------
var marks = [9,12,46,0,56,23,-3,6,35,78,5,68,2,89,11];
var result = marks.filter((x)=>
{
return (x>=35);
})
console.log(result);
[ 46, 56, 35, 78, 68, 89 ]
--------------------------------
var marks = [9,12,46,0,56,23,-3,6,35,78,5,68,2,89,11];
var eresult = marks.filter((x)=>
{
return (x%2==0);
})
console.log(eresult);
[
12, 46, 0, 56,
6, 78, 68, 2
]
---------------------------------------------
var marks = [9,12,46,0,56,23,-3,6,35,78,5,68,2,89,11];
var eresult = marks.filter((x)=>
{
return (x%2==0);
})
console.log(eresult);
eresult.sort((a,b)=>{
return (a-b);
})
console.log(eresult);
------------------------------
var marks = [9,12,46,0,56,23,-3,6,35,78,5,68,2,89,11];
var eresult = marks.filter((x)=>
{
return (x%2==1);
})
console.log(eresult);
[ 9, 23, 35, 5, 89, 11 ]
-------------------------------
22nd March
----------
Q1. What is JavaScript? What is the role of JavaScript engine?
Q2. What are client side and server side?
Q3. What are variables? What is the difference between var, let, and const ?
Q4. What are data types in JS?
Q5. What are operators? What are the types of operators in JS?
Q6. What are the types of conditions statements in JS?
Q7. What is a loop? What are the types of loops in JS?
Q8. What are Functions in JS? What are the types of function?
Q9. What are Arrow Functions in JS? What is it use?
Q10. What are Arrays in JS? How to get, add & remove elements from arrays?
Q11. What is Scope in JavaScript?
Q12. What is the use of typeof operator?
Q13. What is the difference between == and ===?
Q14. What is the indexOf() method of an Array?
Q15. What is the difference between for…of and for…in loop?
Q16. What are Callback Functions? What is it use?
Q17. What is Higher-order function In JS?
Q18. What is the difference between arguments and parameters?
Coding:
Q1. Write a function to remove duplicate elements(type is your choice) from an array.
Q2. Write a function to find the largest number in an array.
Q3. Write a function to check if a given number is prime or not?
Q4. Write a function to calculate the factorial of a number.
--------------------------------------------------------------
var marks = [9,12,46,0,56,23,-3,6,35,78,5,68,2,89,11];
var result =marks.map((v)=>{
return "" + v + "" ;
})
console.log(result);
[
'9',
'12',
'46',
'0',
'56',
'23',
'-3',
'0',
'56',
'23',
'-3',
'6',
'35',
'78',
'5',
'68',
'0',
'56',
'23',
'-3',
'6',
'35',
'78',
'5',
'0',
'56',
'23',
'-3',
'6',
'56',
'23',
'-3',
'23',
'-3',
'-3',
'6',
'35',
'78',
'5',
'68',
'2',
'89',
'11'
]
--------------------------------------
var marks = [9,12,46,0,56,23];
var result =marks.map((v,i,a)=>{
console.log(v + ' is at ' + i + '-- ' + a);
return "" + v + "" ;
})
console.log(result);
-------------------------------------------------------
recursion
---------
4 - n
n*(n-1)
4*3*2*1
12*2*1
24*1
24
- a function calling itself
-
----------------------
var n = 5;
var r = 1;
if(n>0)
{
for(i = 1;i<=n;i++)
{
r = r*i
}
console.log(r);
}
else
{
console.log("Invalid Input");
}
------------------------------------------
var n = 5;
function FindFact(n)
{
if(n == 1)
{
return 1;
}
else
{
return n * FindFact(n-1);
}
}
var r = FindFact(n);
console.log(n + " it's recursion value " + r)
// 5 it's recursion value 120
--------------------------------------------