undefined, null, boolean, string, symbol, number, and object
var a =1;
console.log(a);{var myName ='Jim';// used in the whole program
console.log(myName);}{
console.log(myName);}{let yourName ="Lily";// used in the scope you declared
console.log(yourName);}{// console.log(yourName); // yourName is not defined}{const pi =3.14
console.log(pi);// pi = 3.1415 // error of assignment of const variable}{// console.log(pi); // const used in the same scope and cannot be changed}
4 declaration and assignment
var a;var b =2;// can change later
console.log(a);
console.log(b);
a =1;
b =3;
console.log(a);
console.log(b);
›undefined
›2
›1
›3
5 string plus and number plus
var a =5;var b =15;var c ="I am a ";
a = a +1;
b = b +1;
c = c +"string";
console.log(a);
console.log(b);
console.log(c);
›6
›16
›I am a string
6 case sensitive
var a =5;varB=15;
console.log(a);// console.log(b); // error b is not defined, variable use camel case
7 add 2 numbers
var sum =10+10;
console.log(sum);
8 subtracting 2 numbers
var sub =10-10;
console.log(sub);
9 multiplying 2 numbers
var multi =10*10;
console.log(multi);
10 dividing 2 numbers
var div =10/10;
console.log(div);
11 incrementing a number
var number =10;
number = number +1;
number +=1;
number++;
console.log(number);
12 decrementing numbers
var number =10;
number = number -1;
number -=1;
number--;
console.log(number);
13 decimal numbers
var decimal =10.999;
decimal--;
console.log(decimal);
14 multiply decimal numbers
var product =2.0*0.0;
console.log(product);
15 divide decimal numbers
var quotient =2.0/1.0;
console.log(quotient);
16 finding a remainder
var remainder;
remainder =11%3;
console.log(remainder);
17 compound assignment with augmented addition
var a =1;var b =1;var c =1;
a = a +12;
b = b +12;
c = c +12;
a +=12;
b +=12;
c +=12;
console.log(a);
console.log(b);
console.log(c);
18 compound assignment with augmented subtraction
var a =1;var b =1;var c =1;
a = a -12;
b = b -12;
c = c -12;
a -=12;
b -=12;
c -=12;
console.log(a);
console.log(b);
console.log(c);
19 compound assignment with augmented multiplication
var a =1;var b =1;var c =1;
a = a *12;
b = b *12;
c = c *12;
a *=12;
b *=12;
c *=12;
console.log(a);
console.log(b);
console.log(c);
20 compound assignment with augmented division
var a =8;var b =8;var c =8;
a = a /2;
b = b /2;
c = c /2;
a /=2;
b /=2;
c /=2;
console.log(a);
console.log(b);
console.log(c);
21 declaring string variables
var firstName ="Alan";var lastName ="Turing";
console.log(firstName);
console.log(lastName);
22 escaping literal quotes in strings
var myStr ="I am a \"double quoted\" string inside \"double quotes\"";// backslash
console.log(myStr);var httpStr ="<a hret=\"http://www.baidu.com\" target=\"_blank\">Link</a>"// backslash
console.log(httpStr);var httpStr1 ='<a hret="http://www.baidu.com" target="_blank">Link</a>'// single quotes
console.log(httpStr1);var httpStr2 =`'<a hret="http://www.baidu.com" target="_blank">Link</a>'`// backtsgs
console.log(httpStr2);
23 escaping sequences in string
/*
\' single quote
\" double quote
\\ backslash
\n newline
\r carriage return
\t tab
\b backspace
\f form feed
*/var my ="firstLine\n\t\\secondLine\nthirdLine";
console.log(my);
24 concatenating strings with plus assignment
var my ="firstLine ";var your ="secondLine";var his = my + your;
console.log(his);
25 concatenating strings with plus equal assignment
var my ="firstLine ";
my +="secondLine";
console.log(my);
26 concatenating strings with variables
var myName ="Jim";var my ="My name is "+ myName;
console.log(my);
27 appending variables to strings
var myName ="Jim";var my ="My name is ";
my += myName;
console.log(my);
functiontestValue(val){if(val >100){return"over 100";}if(val >10){return"over 10";}return"10 or under";}
console.log(testValue("12"));functiontestValue(val){if(val >=100){return"over 100 and 100";}if(val >=10){return"10 and over 10";}return"under";}
console.log(testValue("12"));functiontestValue(val){if(val <25){return"under 25";}if(val <55){return"under 55";}return"55 and over";}
console.log(testValue("12"));functiontestValue(val){if(val <=25){return"25 and under 25";}if(val <=55){return"55 andunder 55";}return"over 55";}
console.log(testValue("12"));
54 logic and, or and not
functiontestValue(val){if(val <=55){if(val >=25){return"25 to 55";}}return"other";}// this is better
console.log(testValue("30"));functiontestValue(val){if(val <=55&& val >=25){return"25 to 55";}return"other";}
console.log(testValue("30"));functiontestValue(val){if(val <=10|| val >=20){return"outside"}return"inside";}
console.log(testValue("15"));functiontestValue(val){if(!(val >=10&& val <=20)){return"outside"}return"inside";}
console.log(testValue("15"));
functiontestValue(val){if(val <5){return"inside"}elseif(val <10){return"midlle";}else{return"outside";}}
console.log(testValue("11"));// errorfunctiontestValue(val){// order is importantif(val <10){return"middle"}elseif(val <5){return"inside";}else{return"outside";}}
console.log(testValue("3"));// should be inside
functionisLess(a, b){// fix this codeif(a < b){returntrue;}else{returnfalse;}}
console.log(isLess(10,15));// this code is betterfunctionisLess(a, b){return a < b;}
console.log(isLess(10,15));
63 undefined
functionabTest(a, b){if(a <0|| b <0){return undefined;}return Math.round(Math.pow(Math.sqrt(a)+ Math.sqrt(b),2))}
console.log(abTest(-2,2));
var Dog ={"name":"Jim","leg":4,"tail":1,"friends":["everything!"]};
console.log(Dog);
66 accessing object properties
var Dog ={"name":"Jim","leg":4,"tail":1,"friends":["everything!"],"the drinks":"water"};
console.log(Dog.name);
console.log(Dog["the drinks"]);// if there is blank in key, use []
67 assign key to variable
var Dog ={12:"Jim",15:4,16:1,17:["everything!"],18:"water"};var number =18;
console.log(Dog[number]);// if there is blank in key, use []
68 updating objects
var Dog ={"name":"Jim"};
Dog.name ="Lily";
console.log(Dog);
69 adding and deleting properties from objects
var Dog ={"name":"Jim"};
Dog.name ="Lily";
Dog.color ="black";
console.log(Dog);delete Dog.color;
console.log(Dog);
70 using objects for loops
// object is better than switchfunctionphoneticLookup(val){var result ="";switch(val){case"alpha":
result ="Adams";break;case"bravo":
result ="Boston";break;case"charlie":
result ="Chicago";break;case"delta":
result ="Denver";break;case"echo":
result ="Easy";break;case"foxtrot":
result ="Frank";break;}return result;}
console.log(phoneticLookup("alpha"));// object methodvar result ="";functionphoneticLookup(val){var lookup ={"alpha":"Adams","bravo":"Boston","charlie":"Chicago","delta":"Denver","echo":"Easy","foxtrot":"Frank"};
result = lookup[val];return result;}
console.log(phoneticLookup("alpha"));
71 testing objects for properties
var myObj ={"alpha":"Adams","bravo":"Boston","charlie":"Chicago","delta":"Denver","echo":"Easy","foxtrot":"Frank"};functioncheckObj(checkProp){if(myObj.hasOwnProperty(checkProp)){return myObj[checkProp];}else{return"Not found"}}
console.log(checkObj("alpha"));
72 complex objects
var myMusic =[{"artist":"Adams","title":"Boston","released_year":2005,"formats":["CD","8T","LP"],"gold":true},{"artist":"Adam","title":"Bost","released_year":2003,"formats":["C","8","L"],"gold":true}];
console.log(myMusic);
73 accessing property
var myMusic ={// use dot"artist":"Adams","title":"Boston","released_year":2005,"formats":["CD","8T","LP"],"gold":true}
console.log(myMusic.formats[0]);
74 while loop
var myArray =[];var i =0;while(i <5){
myArray.push(i);
i++;}
console.log(myArray);// ›[0, 1, 2, 3, 4]
75 for loop
var myArray =[];for(var i =0; i <5; i++){
myArray.push(i);}
console.log(myArray);// ›[0, 1, 2, 3, 4]var myArray =[];for(var i =0; i <10; i +=2){
myArray.push(i);}
console.log(myArray);var myArray =[];for(var i =10; i >0; i--){
myArray.push(i);}
console.log(myArray);// ›[10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
76 iterate contents of an array
var myArray =[1,2,3,4,5];for(var i =0; i <5; i++){
console.log(myArray[i]);}var myArray =[1,2,3,4,5];for(var i =0; i < myArray.length; i++){
console.log(myArray[i]);}
77 nesting for loops
var myArray =[[1,2],[3,4],[5,6,7]];for(var i =0; i < myArray.length; i++){for(var j =0; j < myArray[i].length; j++)
console.log(myArray[i][j]);}
78 do while loop
var myArray =[];var i =0;do{
myArray.push(i);
i++;}while(i <5);
console.log(myArray);
79 look up contact file
var contacts =[// can be many{"firstName":"A","lastName":"B","number":"0001","likes":["pizza","coding"]}]functionlookUpProfile(name, prop){for(var i =0; i < contacts.length; i++){if(contacts[i].firstName === name){return contacts[i][prop]||"No such property";}}return"No such contact";}var data =lookUpProfile("A","likes");
console.log(data);
functioncheckEqual(a, b){return a === b ?true:false;// return a === b;}
console.log(checkEqual(1,2));
85 multiple ternary operator
functioncheckSign(num){return num >0?"positive": num <0?"negative":"zero";}
console.log(checkSign(0));
86 difference between var and let
// let is used in the scope of functon, var is used as global variablelet catName ="Jim";let quote;// let catName = "Lily"; let cannot define same name twice
catName ="Lily";// okfunctioncatTalk(){"use strict";
catName ="Oliver";
quote = catName +" says Meow!";return quote;}
console.log(catTalk());
87 const cannot change
const catName ="Jim";// errorlet quote;// let catName = "Lily"; let cannot define same name twice
catName ="Lily";// okfunctioncatTalk(){"use strict";
catName ="Oliver";
quote = catName +" says Meow!";return quote;}
console.log(catTalk());
88 const array
const array =[1,2,3];functioncheck(){"use strict";// array = [1, 2, 2]; // not ok
array[1]=3;// ok}check();
console.log(array);
89 prevent object mutation
functionfreezeObj(){"use strict";constMATH_CONSTANTS={PI:3.14};
Object.freeze(MATH_CONSTANTS);try{MATH_CONSTANTS.PI=99;}catch( ex ){
console.log(ex);}returnMATH_CONSTANTS.PI;}constP=freezeObj();
console.log(P);
const realNumberArray =[4,5.6,-9.8,4,5,6,7];constsquareList=(arr)=>{// filter: keep in the list of filter condition, map : key in pythonconst squareIntegers = arr.filter(num => Number.isInteger(num)&& num >0).map(x => x * x);return squareIntegers;};const squareIntegers =squareList(realNumberArray);
console.log(squareIntegers);
93 write higher order arrow function
const increment =(function(){returnfunctionincrement(number, value =1){return number + value;}})();
console.log(increment(5,2));
console.log(increment(5));
94 use the rest operator with function parameters
const sum =(function(){returnfunctionsum(x, y, z){const args =[x, y, z];return args.reduce((a, b)=> a + b,0);};})();
console.log(sum(1,2,3));// before only 3 parametersconst sum =(function(){returnfunctionsum(...args){return args.reduce((a, b)=> a + b,0);};})();
console.log(sum(1,2,3,4));// now many parameters
95 use the spread operator to evaluate array in -place
const arr1 =["a","b","c","d","e"];let arr2;(function(){
arr2 = arr1;
arr1[0]="potato";})();
console.log(arr2);// the result will be ["potato", "b", "c", "d", "e"]const arr1 =["a","b","c","d","e"];let arr2;(function(){
arr2 =[...arr1];// spread
arr1[0]="potato";})();
console.log(arr2);// the result will be ["a", "b", "c", "d", "e"]
96 use destructuring assignments to assign var from objects
var voxel ={x:3, y:4, z:5};// var x = voxel.x;// var y = voxel.y;// var z = voxel.z;const{x: a, y: b, z: c}= voxel;
console.log(a);