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