You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
console.log(typeof42);// "number"console.log(typeof"hello");// "string"console.log(typeoftrue);// "boolean"console.log(typeofundefined);// "undefined"console.log(typeofnull);// "object" (this is a known bug in JavaScript)console.log(typeofSymbol());// "symbol"console.log(typeof{});// "object"console.log(typeof[]);// "object"console.log(typeoffunction(){});// "function"
letx=10;x+=5;// x = x + 5console.log(x);// 15x-=3;// x = x - 3console.log(x);// 12x*=2;// x = x * 2console.log(x);// 24x/=4;// x = x / 4console.log(x);// 6x%=4;// x = x % 4console.log(x);// 2x**=3;// x = x ** 3console.log(x);// 8
Control Flow
If-Else Statement
letage=18;if(age>=18){console.log("You are an adult");}elseif(age>=13){console.log("You are a teenager");}else{console.log("You are a child");}
Switch Statement
letday="Monday";switch(day){case"Monday":
console.log("Start of the work week");break;case"Friday":
console.log("End of the work week");break;case"Saturday":
case"Sunday":
console.log("Weekend");break;default:
console.log("Midweek");}
letarr=[1,2,3,4,5];// Adding elementsarr.push(6);// Adds to the endarr.unshift(0);// Adds to the beginning// Removing elementsarr.pop();// Removes from the endarr.shift();// Removes from the beginning// Finding elementsconsole.log(arr.indexOf(3));// Returns 2console.log(arr.includes(3));// Returns true// Transforming arraysletdoubled=arr.map(num=>num*2);letevenNumbers=arr.filter(num=>num%2===0);letsum=arr.reduce((total,num)=>total+num,0);// Iteratingarr.forEach(num=>console.log(num));// Sortingarr.sort((a,b)=>a-b);// Ascending order
Objects
Creating Objects
letperson={name: "John",age: 30,greet: function(){console.log("Hello, my name is "+this.name);}};// Accessing propertiesconsole.log(person.name);// "John"console.log(person['age']);// 30// Calling methodperson.greet();// "Hello, my name is John"
classPerson{constructor(name,age){this.name=name;this.age=age;}greet(){console.log(`Hello, my name is \${this.name}`);}}letjohn=newPerson("John",30);john.greet();// "Hello, my name is John"
try{// Code that may throw an errorthrownewError('Something went wrong');}catch(error){console.error(error.message);}finally{console.log('This always runs');}
Custom Errors
classCustomErrorextendsError{constructor(message){super(message);this.name='CustomError';}}try{thrownewCustomError('This is a custom error');}catch(error){console.error(error.name+': '+error.message);}