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
letfirstName="John";// Declaring and initializing a string variableletage=25;// Declaring and initializing a number variableletisStudent=true;// Declaring and initializing a boolean variableletgrades=[90,85,92];// Declaring and initializing an array variable// Declaring and initializing an object variableletperson={name: "Alice",age: 30,};
Variable Naming Rules
letcamelCaseVariable;// Camel case is commonly used (e.g., camelCase)letsnake_case_variable;// Snake case is an alternative (e.g., snake_case)constPI=3.14;// Constants are usually written in uppercase
Scope
functionexampleFunction(){letlocalVar="I am a local variable";// Local variable within the functionconsole.log(localVar);}exampleFunction();// Output: "I am a local variable"console.log(localVar);// Error: localVar is not defined outside the function
Hoisting
console.log(hoistedVar);// Output: undefinedvarhoistedVar="I am hoisted";// Variable declaration is hoisted to the top
letmessage=`Hello, ${firstName}! You are ${age} years old.`;console.log(message);// Output: "Hello, John! You are 25 years old."
Dynamic Typing
letdynamicVariable="I am a string";dynamicVariable=42;// Can change type dynamicallyconsole.log(dynamicVariable);// Output: 42
Null and Undefined
letnullVariable=null;// Represents intentional absence of any object valueletundefinedVariable;// Variable has been declared but not definedconsole.log(nullVariable);// Output: nullconsole.log(undefinedVariable);// Output: undefined
typeof Operator
console.log(typeofage);// Output: "number"console.log(typeofisStudent);// Output: "boolean"// Examplesletx=5;lety=10;letsum=x+y;// Sum is 15console.log(sum);// Output: 15
Concatenating Strings
letgreeting="Hello";letname="World";letwelcomeMessage=greeting+", "+name+"!";console.log(welcomeMessage);// Output: "Hello, World!"// Array Operationsgrades.push(88);// Adds 88 to the end of the grades arrayconsole.log(grades);// Output: [90, 85, 92, 88]// Object Propertiesconsole.log(person.name);// Output: "Alice"person.location="City A";// Adding a new property to the person objectconsole.log(person);// Output: { name: 'Alice', age: 30, location: 'City A' }