Kay Ashaolu
var
, let
, or const
let name = "John Doe";
const PI = 3.14159;
var age = 30; // Avoid using var in modern JavaScript
Primitive Types
Object (Reference Type)
// Numbers
let integer = 42;
let float = 3.14;
// Strings
let singleQuotes = 'Hello';
let doubleQuotes = "World";
let templateLiteral = `Hello, ${name}!`;
// Booleans
let isTrue = true;
let isFalse = false;
// Null and Undefined
let emptyValue = null;
let undefinedValue;
console.log(undefinedValue); // Output: undefined
function greet(name) {
return `Hello, ${name}!`;
}
console.log(greet("Alice")); // Output: Hello, Alice!
// Arrow function (ES6)
const multiply = (a, b) => a * b;
console.log(x); // Output: undefined
var x = 5;
// The above is interpreted as:
var x;
console.log(x);
x = 5;
let fruits = ["apple", "banana", "orange"];
console.log(fruits); // Output: banana
fruits.push("grape");
console.log(fruits.length); // Output: 4
push()
, pop()
: Add/remove from endunshift()
, shift()
: Add/remove from beginningslice()
, splice()
: Extract/modify portionsmap()
, filter()
, reduce()
: Transform arrayslet numbers = [1, 2, 3, 4, 5];
let doubled = numbers.map(num => num * 2);
console.log(doubled); // Output: [2, 4, 6, 8, 10]
let person = {
name: "John Doe",
age: 30,
isStudent: false
};
console.log(person.name); // Output: John Doe
console.log(person["age"]); // Output: 30
let calculator = {
add: function(a, b) {
return a + b;
},
subtract: (a, b) => a - b
};
console.log(calculator.add(5, 3)); // Output: 8
console.log(calculator.subtract(10, 4)); // Output: 6
// Array destructuring
let [first, second, ...rest] = [1, 2, 3, 4, 5];
console.log(first, second, rest); // Output: 1 2 [3, 4, 5]
// Object destructuring
let { name, age } = person;
console.log(name, age); // Output: John Doe 30
let arr1 = [1, 2, 3];
let arr2 = [...arr1, 4, 5];
console.log(arr2); // Output: [1, 2, 3, 4, 5]
let obj1 = { x: 1, y: 2 };
let obj2 = { ...obj1, z: 3 };
console.log(obj2); // Output: { x: 1, y: 2, z: 3 }
let jsonString = '{"name": "Alice", "age": 25}';
let parsedObject = JSON.parse(jsonString);
console.log(parsedObject.name); // Output: Alice
let newJsonString = JSON.stringify(parsedObject);
console.log(newJsonString); // Output: {"name":"Alice","age":25}
Questions? Let's discuss!