javascript -high level language (now) -it can be used as client side and server side -not type safe, loosly typed language -no main() -case sensitive -initially is function oriented but now it support oops -extension is .js -interpreter based -every web browser uses javascript execution engine as a part of them datatypes -there are no explicit datatypes -to declare a variable, data type is not required. -console object is used for output primitive / abstract value type / reference predefined / user defined -implicit datatypes -alone variable must be initialized to use it. -every variable must declard or initialized to use it. - type of variable is decided based on the value assigned to it. - any variables value and type can changed number - when a variable is assigned with numerical value of any kind(floating point & non-floating) string - when a variable is assigned with a value enclosed by single quotes or double quotes boolean - when a variable is assigned with true or false object - when a variable is assigned with null or [] or {} undefined - when a variable is assigned with undefined function - when a variable is assigned with function bigint - when a variable is assigned with BigInt() Price console.log(Price); ReferenceError: Price is not defined ---------------------------------------- Price=123.99; console.log(Price + ' ' + typeof(Price)); ---------------------------------------- Price=123.99; console.log(Price + ' ' + typeof(Price)); Price = 'ashok it'; console.log(Price + ' ' + typeof(Price)); Price = "JavaScript"; console.log(Price + ' ' + typeof(Price)); Price = true; console.log(Price + ' ' + typeof(Price)); Price = false; console.log(Price + ' ' + typeof(Price)); ----------------------------------------- Price = null; console.log(Price + ' ' + typeof(Price)); Price = []; console.log(Price + ' ' + typeof(Price)); Price = {}; console.log(Price + ' ' + typeof(Price)); --------------------------------------------- function Add(x, y) { console.log(x+y); } Add(99,29); Price = Add; console.log(Price + ' ' + typeof(Price)); Price(9,39); ------------------------------------------------ Price= Number(); console.log(Price + ' ' + typeof(Price)); ------------------------------------------ Price= Number(199); console.log(Price + ' ' + typeof(Price)); ------------------------------------------- Price= Number('test'); console.log(Price + ' ' + typeof(Price)); -------------------------------------------- Price= String(); console.log(Price + ' ' + typeof(Price)); --------------------------------------------- Price= String('ashok it'); console.log(Price + ' ' + typeof(Price)); -------------------------------------------- Price= Boolean(); console.log(Price + ' ' + typeof(Price)); ------------------------------------------- Price= Boolean(true); console.log(Price + ' ' + typeof(Price)); ------------------------------------------- Price= Object(); console.log(Price + ' ' + typeof(Price)); --------------------------------------------- Price= Object(299); console.log(Price + ' ' + typeof(Price)); ------------------------------------------- Price= Object('ashok it'); console.log(Price + ' ' + typeof(Price)); ------------------------------------------- Price= Object(true); console.log(Price + ' ' + typeof(Price)); ------------------------------------------ Price= Object(null); console.log(Price + ' ' + typeof(Price)); -------------------------------------------- Price= Date(); console.log(Price + ' ' + typeof(Price)); ------------------------------------------- declare variable var let var Price = 99; console.log(Price + ' ' + typeof(Price)); ------------------------------------------ var Price; console.log(Price + ' ' + typeof(Price)); ------------------------------------------ let Price; console.log(Price + ' ' + typeof(Price)); ----------------------------------------- let Price=99; console.log(Price + ' ' + typeof(Price)); ----------------------------------------- Operators Arithmetic + - * / % ** 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); ---------------------- + positive - negative var x = +10; var y = -2; console.log(x); console.log(y); ---------------------- z = x + y; x + y ----> arithmetic expression x y ----> operands + -------> operator binary operator unary operator ------------------------- ++ increment / decrement (pre/post) -- var x = 10; var y = ++x; // increment first , assign later console.log(x + ' ' + y); ------------------------------ var x = 10; var y = --x; // decrement first , assign later console.log(x + ' ' + y); ------------------------------ var x = 10; var y = x--; // assign first , decrement later console.log(x + ' ' + y); ------------------------------ var x = 10; var y = x++; // assign first , increment later console.log(x + ' ' + y); ------------------------------ short-cut operators += -= *= /= %= var x = 10; //x = x + 2; //x = 10 +2; //x = 12; 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 - comparison ------------------ > < >= <= == === != Note: always the return value of a valid relative expression is boolean (true / false) var x = 10; var y = 2; var z = 10; console.log(x>y); console.log(x>z); console.log(x>=z); console.log(x false { console.log(x +' is less than ' + y); } else if(x>y) // 10>10 -> false { console.log(x + ' is greater than ' + y); } else { console.log(x + ' is equal to ' + y); } -------------------------------------------- var x = 10; var y = 10; if(x false { console.log(x +' is less than ' + y); } else if(x>y) // 10>10 -> false { console.log(x + ' is greater than ' + y); } else if(x == y) { console.log(x + ' is equal to ' + y); } ------------------------------------------------- switch(variable/other) { case value1: // logic break; case value2: //logic break; case value3: //logic break; ----------- ----------- ----------- ----------- default: //logic - executed when no case value is matched with switch variable's value break; } ---------------- var UserType="employee"; switch(UserType) { case "customer": // if(UserType=='customer') console.log('load customer menu'); break; case "employee": console.log('load employee menu'); break; case 'admin': console.log('load admin menu'); break; case 'guest': console.log('load guest menu'); break; } -------------------- var UserType="vendor"; switch(UserType) { case "customer": // if(UserType=='customer') console.log('load customer menu'); break; case "employee": console.log('load employee menu'); break; case 'admin': console.log('load admin menu'); break; case 'guest': console.log('load guest menu'); break; default: console.log('invalid usertype ...check once'); break; } ----------------------------------- var UserType="Customer"; UserType = UserType.toLowerCase(); switch(UserType) { case "customer": // if(UserType=='customer') console.log('load customer menu'); break; case "employee": console.log('load employee menu'); break; case 'admin': console.log('load admin menu'); break; case 'guest': console.log('load guest menu'); break; default: console.log('invalid usertype ...check once'); break; } ------------------------------------------------------ var v = 89; switch(true) { case v>=90: console.log('A+'); break; case v>=80: console.log('A'); break; case v>=70: console.log('B'); break; case v>=60: console.log('C'); break; case v>=50: console.log('D'); break; case v>=35: console.log('E'); break; default: console.log('F'); break; } ----------------------------------------------------- iterative cs - loops: a.while b.do..while c.for d.for..in -used to execute one or more statements again and again based on a condition -write once use many times -avoids duplication of code -re-usability 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) { console.log(i + ' inside while ' + (i<=6)); i = i +1; } console.log(i + ' outside while ' + (i<=6)); 1 inside while true 2 inside while true 3 inside while true 4 inside while true 5 inside while true 6 inside while true 7 outside while false -------------------------- var i =11; while(i<=6) { console.log(i + ' inside while ' + (i<=6)); i = i +1; } console.log(i + ' outside while ' + (i<=6)); 11 outside while false -------------------------------------- var i =1; do { console.log(i + ' inside while ' + (i<=6)); i = i +1; }while(i<=6); console.log(i + ' outside while ' + (i<=6)); -------------------------------------------------- var i =11; do { console.log(i + ' inside while ' + (i<=6)); i = i +1; }while(i<=6); console.log(i + ' outside while ' + (i<=6)); 11 inside while false 12 outside while false ------------------------------------------ var i; for(i =1;i<=6;i = i +1) { console.log(i + ' inside for ' + (i<=6)); } console.log(i + ' outside for ' + (i<=6)); 1 inside for true 2 inside for true 3 inside for true 4 inside for true 5 inside for true 6 inside for true 7 outside for false ------------------------------------------------ var i; for(i =11;i<=6;i = i +1) { console.log(i + ' inside for ' + (i<=6)); } console.log(i + ' outside for ' + (i<=6)); 11 outside for false -------------------------------- var i =1; for(;i<=6;) { console.log(i + ' inside for ' + (i<=6)); i = i +1; } console.log(i + ' outside for ' + (i<=6)); ------------------------------------------- var i =1; for(;;) { console.log(i + ' inside for ' + (i<=6)); i = i +1; } console.log(i + ' outside for ' + (i<=6)); // unreachable code ---------------------------------------------- var i =1; for(;;) { console.log(i + ' inside for ' + (i<=6)); if(i==6) { break; } i = i +1; } console.log(i + ' outside for ' + (i<=6)); 1 inside for true 2 inside for true 3 inside for true 4 inside for true 5 inside for true 6 inside for true 6 outside for true ------------------------------------------ for...in --- collections - -array -is a collection of values -*same type or can be different type also (primitive/abstract) or arrays -stored in continious memory location -each value is identified by their position called index -where that index value starts with zero and ends with array size minus one -*index is number,can be string also - negative index is not allowed -for example if array is created with a size 10 then index value starts with 0 and ends with 9 -to read value from array arrayname[index] -to assign a value arrayname[index] =value; array creation manage array using array methods array creation 1.using [] 2.using Array() constructor var arrayname = []; // empty array var arrayname = [v1,v2,v3,v4,......vN]; // array with values var marks = [88,99,33,90,11,34,56,33,58,19]; console.log(marks[0]); console.log(marks[1]); // to read console.log("using for: "); for(i=0;i<10;i++) { console.log(marks[i]) } --------------------------- var marks = [88,99,33,90,11,34,56,33,58,19]; console.log(marks[0]); console.log(marks[1]); // to read console.log("using for in: "); for(i in marks) { console.log(i + " " + marks[i]); } ------------------------------------------------ //var marks = [88,99,33,90,11,34,56,33,58,19]; var marks = [88,99,33,90,11,34]; console.log(marks[0]); console.log(marks[1]); // to read console.log("using for in: "); for(i in marks) { console.log(i + " " + marks[i]); } ---------------------------------------------- var Product = [9001,'Apple','1Kg',199.00,true]; console.log(Product); -------------------------------------- var Product = []; // empty array console.log(Product); -------------------------------------- // array with multiple values var Product = Array(9009,'Toor Dal','1kg',299.00,true); console.log(Product); -------------------------------------- // array with single value - // becomes array size not array content var Product = Array(9); console.log(Product); console.log('size ' + Product.length); ----------------------------------------- // array with single value - var Product = [9]; console.log(Product); console.log('size ' + Product.length); [ 9 ] size 1 -------------------------------------------- var Product = [ 9001, 'Toor/Arhar Dal', 'bb Popular', 'Veg', true, '10000428_17-bb-popular-toorarhar-dal.jpg', '500g', '1kg', '2kg', 140, 275, 549, 38, 48, 44, 87.5, 144, 309, true, true, true ]; console.log(Product); console.log('size ' + Product.length); // size 21 ------------------------------------------------------ Product ProductId:single value ProductName : sv Brand : sv Type : sv IsActive: sv ImagePath :sv Quantity : multiple values Mrp : multiple values Offer:multiple values SalePrice: multiple values QStatus:multiple values --------------------------------------- var Quantity = ['500g','1kg','2kg']; var Mrp = [140,275,549]; var Offers = [38,48,44]; var SalePrice = [87.5,144,309]; var QStatus = [true,true,true]; var Product = [ 9001, 'Toor/Arhar Dal', 'bb Popular', 'Veg', true, '10000428_17-bb-popular-toorarhar-dal.jpg', Quantity, Mrp, Offers, SalePrice, QStatus ]; console.log(Product); console.log('size ' + Product.length); console.log('Product Id : ' + Product[0]); console.log('Name : ' + Product[1]); console.log('Quantity :' + Product[6]); console.log('Quantity :' + Product[6][0]); console.log('Quantity :' + Product[6][1]); console.log('Quantity :' + Product[6][2]); -------------------------------------------------- 21st ----- var Quantity = Array('500g','1kg','2kg'); var Mrp = Array(140,275,549); var Offers = Array(38,48,44); var SalePrice = Array(87.5,144,309); var QStatus = Array(true,true,true); var Product = [ 9001, 'Toor/Arhar Dal', 'bb Popular', 'Veg', true, '10000428_17-bb-popular-toorarhar-dal.jpg', Quantity, Mrp, Offers, SalePrice, QStatus ]; console.log(Product); console.log('size ' + Product.length); console.log('Product Id : ' + Product[0]); console.log('Name : ' + Product[1]); console.log('Quantity :' + Product[6]); console.log('Quantity :' + Product[6][0]); console.log('Quantity :' + Product[6][1]); console.log('Quantity :' + Product[6][2]); --------------------------------------------- // try to a value from non-existing location var marks = [99,38,68,39,44,96]; console.log(marks); console.log(marks[0]); console.log('size ' + marks.length); // try to a value from non-existing location console.log(marks[9]); // undefined ------------------------------------------------- // assigning a value to a non-existing location // the given new location is created var marks = [99,38,68,39,44,96]; console.log(marks); console.log(marks[0]); console.log('size ' + marks.length); marks[0] = 100; console.log(marks[0]); marks[9] = 39; console.log('size : ' + marks.length); console.log(marks); [ 100, 38, 68, 39, 44, 96, <3 empty items>, 39 ] -------------------------------------------------- // array is dynamic var marks = []; console.log(marks); console.log('size ' + marks.length); marks[0] = 88; console.log(marks); console.log('size ' + marks.length); marks[1] = 99; console.log(marks); console.log('size ' + marks.length); marks[2] = 44; console.log(marks); console.log('size ' + marks.length); ------------------------------------- var data = Array(0); console.log(data); console.log(data.length); -------------------------- var data = Array(); console.log(data); console.log(data.length); --------------------------- //RangeError: Invalid array length var data = Array(-3); console.log(data); console.log(data.length); ---------------------------- var Product = []; console.log(Product + ' ' + Product.length); Product['ProductId'] = 9009; Product['Name'] = 'Organic Indian Toor Dal'; Product['Quantity'] = '2Kg'; Product['Price'] = 399; console.log(Product); console.log(Product + ' ' + Product.length); console.log(Product[0]); // undefined console.log(Product['ProductId']); console.log(Product['Name']); console.log(Product['Quantity']); console.log(Product['Price']); -------------------------------- array methods ------------- a.creation b.managing the array values 1.add 2.remove 3.search 4.sort 5.other to add and remove ----------------- push() - used to append the given value/values/array at the end of the array. it returns new size unshift() -used to insert the given value/values/array at the top of the array. it returns new size pop() - used to remove and return the value from the end of an array. - when pop is applied on an empty array , it returns undefined. shift() - used to remove and return the value from the top of an array. - when shift is applied on an empty array , it returns undefined. splice() - used to insert/remove/replace includes() - searches for the given value , from the given index(starting index for search) if found returns true, not found returns false. // array methods - push with single value var marks = [99,39,29,49,56,79]; console.log(marks.length); console.log(marks.push(69)); console.log(marks); [ 99, 39, 29, 49, 56, 79, 69 ] ------------------------ // array methods - push with multiple value var marks = [99,39,29,49,56,79]; console.log(marks.length); console.log(marks.push(69,19,29)); console.log(marks.length); console.log(marks); [ 99, 39, 29, 49, 56, 79, 69, 19, 29 ] ------------------------- // array methods - push with array var marks = [99,39,29,49,56,79]; console.log(marks.length); console.log(marks.push([69,19,29])); console.log(marks.length); console.log(marks); [ 99, 39, 29, 49, 56, 79, [ 69, 19, 29 ] ] -------------------------------------------- 22nd ---- var marks = [99,39,29,49,56,79]; // unshift with single value console.log('size before:' + marks.length); console.log(marks.unshift(69)); console.log('size after:' + marks.length); console.log(marks); [ 69, 99, 39, 29, 49, 56, 79 ] ----------------------------------------------- var marks = [99,39,29,49,56,79]; // unshift with multile values console.log('size before:' + marks.length); console.log(marks.unshift(69,33,19)); console.log('size after:' + marks.length); console.log(marks); [ 69, 33, 19, 99, 39, 29, 49, 56, 79 ] -------------------------------------------- var marks = [99,39,29,49,56,79]; // unshift with array console.log('size before:' + marks.length); console.log(marks.unshift([69,33,19])); console.log('size after:' + marks.length); console.log(marks); [ [ 69, 33, 19 ], 99, 39, 29, 49, 56, 79 ] ------------------------------------------- var marks = [99,39,29,49,56,79]; // pop with array console.log('size before:' + marks.length); console.log(marks.pop()); // 79 console.log('size after:' + marks.length); console.log(marks); [ 99, 39, 29, 49, 56 ] ----------------------------- var marks = [99,39,29,49,56,79]; // shift with array console.log('size before:' + marks.length); console.log(marks.shift()); console.log(marks); console.log(marks.shift()); console.log(marks.shift()); console.log(marks.shift()); console.log(marks.shift()); console.log(marks.shift()); console.log('size after:' + marks.length); console.log(marks); console.log(marks.shift()); // undefined size before:6 99 [ 39, 29, 49, 56, 79 ] 39 29 49 56 79 size after:0 [] undefined -------------------------------------------- var marks = [99,39,29,49,56,79]; console.log('size before:' + marks.length); // splice as insert method - insert the given value(3rd param) at given index (1st parameter) var result = marks.splice(0,0,69); console.log('size after:' + marks.length); console.log(marks); console.log(result); size before:6 size after:7 [ 69, 99, 39, 29, 49, 56, 79 ] [] ------------------------------ var marks = [99,39,29,49,56,79]; console.log('size before:' + marks.length); // splice as insert method - insert the given value(3rd param) at given index (1st parameter) var result = marks.splice(2,0,69); console.log('size after:' + marks.length); console.log(marks); console.log(result); size before:6 size after:7 [ 99, 39, 69, 29, 49, 56, 79 ] [] -------------------------------- var marks = [99,39,29,49,56,79]; console.log('size before:' + marks.length); // splice as insert method - insert the given value(3rd param) at given index (1st parameter) var result = marks.splice(2,0,699,199,299,399); console.log('size after:' + marks.length); console.log(marks); console.log(result); size before:6 size after:10 [ 99, 39, 699, 199, 299, 399, 29, 49, 56, 79 ] [] ---------------------------------- var marks = [99,39,29,49,56,79]; console.log('size before:' + marks.length); // splice as insert method - insert the given value(3rd param) at given index (1st parameter) var result = marks.splice(2,0,[699,199,299,399]); console.log('size after:' + marks.length); console.log(marks); console.log(result); size before:6 size after:7 [ 99, 39, [ 699, 199, 299, 399 ], 29, 49, 56, 79 ] [] --------------------------------------------------- //splice as a remove -------------------- var marks = [99,39,29,49,56,79]; console.log('size before:' + marks.length); // splice as remove/delete method - deletes the specified number of values(2nd param) // from the given index (1st parameter) var deletedValues = marks.splice(1,3); console.log('size after:' + marks.length); console.log(marks); console.log(deletedValues); size before:6 size after:3 [ 99, 56, 79 ] [ 39, 29, 49 ] --------------------------------------- replace - remove + insert at same place --------------------------------------- var marks = [99,39,29,49,56,79]; console.log('size before:' + marks.length); // splice as replace method var deletedValues = marks.splice(0,1,69); console.log('size after:' + marks.length); console.log(marks); console.log(deletedValues); [ 69, 39, 29, 49, 56, 79 ] [ 99 ] ---------------------------------------- includes() ---------- var marks = [99,39,29,49,9,56,79,44,9,0,-2,18,55,9,26,38,59]; // searches for a given value from starting - 0th index console.log(marks.includes(199)); // false --------------------------------------------- var marks = [99,39,29,49,9,56,79,44,9,0,-2,18,55,9,26,38,59]; // searches for a given value from starting - 0th index console.log(marks.includes(39)); // true ---------------------------------------------- var marks = [99,39,29,49,9,56,79,44,9,0,-2,18,55,9,26,38,59]; // searches for a given value from starting - 0th index console.log(marks.includes(9)); //searches for 9 from 0th index - 4th ---------------------------------------------------------- var marks = [99,39,29,49,9,56,79,44,9,0,-2,18,55,9,26,38,59]; // searches for a given value from starting - 0th index console.log(marks.includes(9,5)); //searches for 2nd 9 from 5th index - 8th //true ------------------------------------------------- var marks = [99,39,29,49,9,56,79,44,9,0,-2,18,55,9,26,38,59]; // searches for a given value console.log(marks.includes(9,9)); //searches for 3rd 9 from 9th index - 13th //true -------------------------------------------------------- var marks = [99,39,29,49,9,56,79,44,9,0,-2,18,55,9,26,38,59]; // searches for a given value console.log(marks.includes(9,14)); //searches for 4th 9 from 14th index - no //false ----------------------------------------------------------- 23rd ---- marks.map() marks.filter() marks.findIndex() marks.sort() ------------ function : - a collection one or more statements indentified by a name - in the program where ever that functionality is required , it is referred by it's name. - write once use many times in many places. - to function we can pass values - a function may return value types of functions in javascript 1.named function 2.un-named function 3.arrow function cases based on input parameters and arguments -------------------------------------------- case 1 : ip and arguments are equal function Add(x, y) // x y - inpuparameters { console.log(x+y); } Add(10,20); // 10 20 - arguments ---------------------------------- case2: arguments are more than ip function Add(x, y) // x - 10 y - 20 inpuparameters { console.log(x+y); } Add(10,20,30,40); // overflow of arguments ----------------------------------------- case 3: arguments are less than ip function Add(x, y) // x - 10 y - ? inpuparameters { console.log(x); // 10 console.log(y); // undefined console.log(x+y); // 10 + undefined=>NaN } Add(10); ------------------------------- based on type of ip or type of arguments --------------------------------------- function Add(x, y) { console.log(x+y); } Add(10,20); // number Add(299.29,39.49); // number Add("ashok","it"); // string Add(true,true); // boolean Add("dotnet",9); // string number Add([1234],[5,6,7,8]); //array // on function responding for all types of calling statements ------------------------------ function Add(x, y) { console.log(x+y); } Add(10,20); var Sum = Add; console.log(Sum + ''); Sum(30,90); Note: function can be assigned as like a value to the variable ---------------------------------------------------------------- function f1() { console.log('i am function f1'); } function f2() { console.log('i am function f2'); } function f3(x,y) { console.log('i am from f3'); x(); y(); } f3(f1,f2); // call back functions Note: a function which is passed as an argument is called as call back function ------------------------------------------------------------------------------- Note: a function which is taking other as input(argument) or returning a function is called HOF. HOF : Higher Order Function function f1() { console.log('i am a function f1'); function f2() { console.log('i am f2 created inside the f1'); } return f2; } var f3 = f1(); // f3 = f2; f3(); ------------------------------------------------- function Add(x,y) { console.log('I am function Add ' + (x+y)); } Add(10,20); function Sum(x) // x = Add { x(100,200); } Sum(Add); // Add is assigned to x ------------------------------------- Note: a named function can be assigned a value to a variable named function's name loses it's identity var Total = function Add(x,y) { console.log('I am function Add ' + (x+y)); } Total(10,20); //Add(100,200); // ReferenceError: Add is not defined --------------------------------------------------------- var Total = function (x,y) { console.log('I am a unnamed function ' + (x+y)); } Total(10,20); ------------------------------------------------------------ // arrow function var Total = (x,y)=> // input area parameters goes to functional body { console.log('I am a arrow function ' + (x+y)); } // functional body Total(10,20); ------------------------------ var courses = ['java','csharp','go','algol','cobol','fotran','python','c++','pascal','vb','fsharp']; console.log(courses); courses.sort(); console.log('after sort: '); console.log(courses); after sort: [ 'algol', 'c++', 'cobol', 'csharp', 'fotran', 'fsharp', 'go', 'java', 'pascal', 'python', 'vb' ] ------------------------------- var courses = ['java','csharp','Go','algol','Cobol','fotran','python','c++','pascal','vb','Fsharp']; console.log(courses); courses.sort(); console.log('after sort: '); console.log(courses); after sort: [ 'Cobol', 'Fsharp', 'Go', 'algol', 'c++', 'csharp', 'fotran', 'java', 'pascal', 'python', 'vb' ] -------------------------------- var marks = [29,23,29,49,9,6,79,44,9,0,-2,18,51,9,26,18,59]; marks.sort(); // sort logic sutiable for strings not for numbers console.log(marks); [ -2, 0, 18, 18, 23, 26, 29, 29, 44, 49, 51, 59, 6, 79, 9, 9, 9 ] -------------------------------- var marks = [29,23,29,49,9,6,79,44,9,0,-2,18,51,9,26,18,59]; //marks.sort(); // sort logic sutiable for strings not for numbers console.log(marks); marks.sort(Check) function Check(x,y) { return x - y; } console.log('after sort : '+ marks); [ 29, 23, 29, 49, 9, 6, 79, 44, 9, 0, -2, 18, 51, 9, 26, 18, 59 ] after sort : -2,0,6,9,9,9,18,18,23,26,29,29,44,49,51,59,79 ---------------------------------------------------------------- var marks = [29,23,29,49,9,6,79,44,9,0,-2,18,51,9,26,18,59]; //marks.sort(); // sort logic sutiable for strings not for numbers console.log(marks); marks.sort(function Check(x,y) { return x - y; }) console.log('after sort : '+ marks); -------------------------------------- var marks = [29,23,29,49,9,6,79,44,9,0,-2,18,51,9,26,18,59]; //marks.sort(); // sort logic sutiable for strings not for numbers console.log(marks); marks.sort(function (x,y) { return x - y; }) console.log('after sort : '+ marks); -------------------------------------- var marks = [29,23,29,49,9,6,79,44,9,0,-2,18,51,9,26,18,59]; //marks.sort(); // sort logic sutiable for strings not for numbers console.log(marks); marks.sort((x,y)=> { return x - y; }) console.log('after sort : '+ marks); ------------------------------------- var marks = [29,23,29,49,9,6,79,44,9,0,-2,18,51,9,26,18,59]; //marks.sort(); // sort logic sutiable for strings not for numbers console.log(marks); marks.sort((x,y)=> { return y - x; }) console.log('after sort : '+ marks); [ 29, 23, 29, 49, 9, 6, 79, 44, 9, 0, -2, 18, 51, 9, 26, 18, 59 ] after sort : 79,59,51,49,44,29,29,26,23,18,18,9,9,9,6,0,-2 --------------------------------------------------------------------- var marks = [29,23,29,49,9,6,79,44,9,0,-2,18,51,9,26,18,59]; var fresult = marks.filter(Check) function Check(v) { return v<35; } console.log(fresult); [ 29, 23, 29, 9, 6, 9, 0, -2, 18, 9, 26, 18 ] //Check(29) 29<35 - true //Check(23) 23<35 - true //Check(29) 29<35 - true //Check(49) 49<35 - false ---------------------------- var marks = [29,23,29,49,9,6,79,44,9,0,-2,18,51,9,26,18,59]; var presult = marks.filter(Check) function Check(v) { return v>=35; } console.log(presult); //[ 49, 79, 44, 51, 59 ] ----------------------------- var marks = [29,23,29,49,9,6,79,44,9,0,-2,18,51,9,26,18,59]; var presult = marks.filter((v)=> { return v>=35; }) console.log(presult); -------------------------- var marks = [29,23,29,49,9,6,79,44,9,0,-2,18,51,9,26,18,59]; var oresult = marks.filter((v)=> { return v%2 ==1; }) console.log(oresult); [ 29, 23, 29, 49, 9, 79, 9, 51, 9, 59 ] ---------------------------- var marks = [29,23,29,49,9,6,79,44,9,0,-2,18,51,9,26,18,59]; var eresult = marks.filter((v)=> { return v%2 ==0; }) console.log(eresult); [ 6, 44, 0, -2, 18, 26, 18 ] ---------------------------------- var Products = ['apple','toor dal','loose rice','mango','chana dal','loose sugar','moong dal','potato','cherry']; var dals = Products.filter((name)=> { return name.endsWith('dal') }); console.log(dals); [ 'toor dal', 'chana dal', 'moong dal' ] ------------------------------------------