JavaScript has three ways to declare variables:
var oldWay = "I'm using var"; // Function scoped, can be redeclared
let modern = "I'm using let"; // Block scoped, can be reassigned
const constant = "I won't change"; // Block scoped, cannot be reassigned
String: Text data
const name = "JavaScript";
const template = `Hello, ${name}`; // Template literals
Number: Numeric data (both integers and floating-point)
const integer = 42;
const float = 3.14;
const scientific = 2.998e8; // Scientific notation
Boolean: true or false
const isValid = true;
const isComplete = false;
Undefined: Variable declared but not assigned
let notAssigned;
console.log(notAssigned); // undefined
Null: Intentional absence of value
const emptyValue = null;
Symbol: Unique and immutable primitive
const uniqueId = Symbol('id');
BigInt: For integers larger than Number can represent
const bigNumber = 9007199254740991n;
Object: Collection of key-value pairs
const person = {
name: "Alice",
age: 30,
greet() {
return `Hello, I'm ${this.name}`;
}
};
Array: Ordered collection of values
const fruits = ["Apple", "Banana", "Cherry"];
console.log(fruits[1]); // "Banana"
const sum = 5 + 3; // 8
const difference = 5 - 3; // 2
const product = 5 * 3; // 15
const quotient = 5 / 3; // 1.6666...
const remainder = 5 % 3; // 2
const exponent = 5 ** 3; // 125 (5³)