Variables are containers for storing data values in a program. They provide named references to memory locations, allowing you to manipulate data. In JavaScript, variables are declared using the keywords var, let, or const.
var a = "I am global";
let b = "I am global";
// Object literal notation
let car = {
make: "Toyota",
model: "Camry",
year: 2022, isElectric: false,
start: function() {
console.log("Engine started!"); } };
- for Loop:
Executes a block of code a specified number of times.
for (let i = 0; i < 5; i++) {
console.log(i); }
// Creating an array
let fruits = ["apple", "banana", "orange", "grape"];
// Accessing elements
console.log(fruits[0]); // Outputs: apple console.log(fruits[2]); // Outputs: orange
console.log(x); // undefined var x = 5; console.log(x); // 5
The first console.log(x) outputs undefined because the declaration is hoisted, but the assignment (x = 5) happens later in the code.�
const multiply = function(a, b) { return a * b; };
�// Invocation const product = multiply(4, 6); console.log('Function Expression - Product:', product);
Executes a function or a piece of code once after a specified delay.
const delayedFunction = () => {
console.log('Delayed function executed!'); };
setTimeout(delayedFunction,2000); // Executes after 2 seconds
const subtract = (a, b) => a - b; / Invocation
const difference = subtract(8, 3);
console.log('Arrow Function - Difference:', difference);
function parentFunction(name, callback){
setTimeout(callback,1000);
console.log("Hey "+name); } function randomFunction(){ console.log("Hey I am callbackfunction"); } parentFunction("Random String",randomFunction);
##Synchronous Callback Function Example
function parentFunction(name, callback){ callback(); console.log("Hey "+name); } function randomFunction(){ console.log("Hey I am callbackfunction"); } �parentFunction("Random String",randomFunction);