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
// Modern variable declarationsletchangeable="Can be reassigned";constconstant="Cannot be reassigned";varoldStyle="Not recommended, function-scoped";// Multiple declarationsleta,b,c;letx=1,y=2,z=3;
Basic Data Types
// Stringletname="John";lettemplate=`Hello ${name}`;// Template literal// Numberletinteger=42;letfloat=3.14;letinfinity=Infinity;letnotANumber=NaN;// BooleanletisTrue=true;letisFalse=false;// Null and Undefinedletempty=null;letnotDefined=undefined;// Symbol (unique identifier)letsymbol=Symbol('description');// BigInt (large numbers)letbigNumber=9007199254740991n;
letx=5;// Basic assignmentx+=3;// x = x + 3x-=2;// x = x - 2x*=4;// x = x * 4x/=2;// x = x / 2x%=3;// x = x % 3x**=2;// x = x ** 2
Comparison Operators
// Regular comparison5>3// Greater than5<3// Less than5>=3// Greater than or equal5<=3// Less than or equal// Equality5=="5"// Equal (with type coercion)5==="5"// Strictly equal (no type coercion)5!="5"// Not equal5!=="5"// Strictly not equal
// Rest parametersfunctionsum(...numbers){returnnumbers.reduce((a,b)=>a+b,0);}// Destructuring parametersfunctionprintUser({name, age}){console.log(`${name} is ${age} years old`);}// Parameter validationfunctiondivide(a,b){if(b===0)thrownewError("Cannot divide by zero");returna/b;}
Advanced Function Concepts
// Immediately Invoked Function Expression (IIFE)(function(){// code})();// Closurefunctioncounter(){letcount=0;returnfunction(){return++count;};}// Higher-order functionfunctionmultiply(factor){returnfunction(number){returnnumber*factor;};}
Arrays & Objects
Array Methods
// Creating arraysletarray=[1,2,3];letnewArray=Array.from("hello");letfilled=newArray(3).fill(0);// Basic operationsarray.push(4);// Add to endarray.pop();// Remove from endarray.unshift(0);// Add to startarray.shift();// Remove from start// Modern array methodsarray.map(x=>x*2);// Transform elementsarray.filter(x=>x>2);// Filter elementsarray.reduce((a,b)=>a+b);// Reduce to single valuearray.find(x=>x===2);// Find elementarray.some(x=>x>2);// Check if some passarray.every(x=>x>0);// Check if all pass
Object Operations
// Object creationconstuser={name: "John",age: 30,sayHi(){return`Hi, I'm ${this.name}`;}};// Object methodsObject.keys(user);// Get keysObject.values(user);// Get valuesObject.entries(user);// Get key-value pairsObject.freeze(user);// Make immutableObject.assign({},user);// Shallow copy// Destructuringconst{ name, age }=user;const{name: userName}=user;
Modern Object Features
// Spread operatorconstclone={ ...user};constmerged={ ...obj1, ...obj2};// Computed propertiesconstprop="name";constobj={[prop]: "John"};// Object shorthandconstname="John";constuserObj={ name };// Same as { name: name }
// Creating promisesconstpromise=newPromise((resolve,reject)=>{// Async operationif(success){resolve('Success!');}else{reject('Error!');}});// Using promisespromise.then(result=>console.log(result)).catch(error=>console.error(error)).finally(()=>console.log('Done'));// Promise methodsPromise.all([promise1,promise2]);// All promisesPromise.race([promise1,promise2]);// First to completePromise.allSettled([promise1,promise2]);// All settledPromise.resolve('Success');// Resolved promisePromise.reject('Error');// Rejected promise