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
// Function Declarationfunctiongreet(name){return"Hello, "+name;}// Function Expressionconstgreet=function(name){return"Hello, "+name;};// Arrow Functionconstgreet=(name)=>{return"Hello, "+name;};// Arrow Function (concise syntax)constgreet=name=>"Hello, "+name;
Control Structures:
// If-Elseif(x>10){console.log("x is greater than 10");}else{console.log("x is not greater than 10");}// Switchswitch(day){case1:
console.log("Monday");break;case2:
console.log("Tuesday");break;default:
console.log("Other day");}// For Loopfor(leti=0;i<5;i++){console.log(i);}// While Loopleti=0;while(i<5){console.log(i);i++;}
Arrays:
letfruits=["Apple","Banana","Cherry"];// Accessing elementsconsole.log(fruits[0]);// Apple// Looping through arrayfruits.forEach(fruit=>{console.log(fruit);});// Adding an elementfruits.push("Durian");// Removing an elementfruits.pop();// Removes last element
Objects:
letperson={firstName: "John",lastName: "Doe",age: 30,fullName: function(){returnthis.firstName+" "+this.lastName;}};// Accessing propertiesconsole.log(person.firstName);// John// Accessing methodsconsole.log(person.fullName());// John Doe// Adding a new propertyperson.gender="male";// Deleting a propertydeleteperson.age;
Events:
// Adding an event listenerdocument.getElementById("myButton").addEventListener("click",function(){alert("Button clicked!");});
Promises (for asynchronous code):
letpromise=newPromise((resolve,reject)=>{// Simulate an async operation (e.g., network request)setTimeout(()=>{resolve("Success!");},1000);});promise.then(result=>{console.log(result);// Success!}).catch(error=>{console.error(error);});