Use let, const, or var to declare variables. let and const are modern and recommended.
let - Block Scoped, Reassignable
Modern way to declare variables. Can be reassigned and updated:
let name = "Alice";
name = "Bob"; // Reassign - OK
let name = "Carol"; // Redeclare - ERROR
let age = 28;
age = 29; // Update - OK
const - Block Scoped, Non-reassignable
Use by default. Values cannot be changed after assignment:
const PI = 3.14159;
PI = 3.14; // ERROR - cannot reassign
const person = {
name: "Alice",
age: 28
};
person.age = 29; // OK - modifying properties
person = {}; // ERROR - cannot reassign
var - Function Scoped (Avoid)
Old way of declaring variables. Avoid in modern code:
var name = "Alice";
var name = "Bob"; // OK - redeclare allowed
Variable Naming Rules
Must start with letter, underscore (_), or dollar sign ($)
Can contain letters, digits, underscores, or dollar signs
Cannot use JavaScript reserved words (let, if, return, etc.)
Are case-sensitive (name and Name are different)
Use camelCase for multi-word names: firstName, lastName, isActive
// Good names
let firstName = "Alice";
let lastName = "Smith";
let isActive = true;
let _privateVar = "private";
// Bad names
let fn = "Alice"; // Too short
let FIRSTNAME = "Alice"; // Use camelCase, not UPPERCASE
let first-name = "Alice"; // Invalid - no hyphens
let const = "Alice"; // Reserved word
Primitive Data Types
Number
Integer or floating-point numbers:
let integer = 42;
let decimal = 3.14;
let negative = -10;
let veryLarge = 1e6; // 1,000,000
let infinity = Infinity;
let notANumber = NaN; // "Not a Number"
String
Text enclosed in quotes. Can use single, double, or backticks:
let single = 'Hello';
let double = "World";
let template = `Hello, ${name}!`; // Template literal with interpolation
// String methods
let text = "Hello";
console.log(text.length); // 5
console.log(text.toUpperCase()); // "HELLO"
console.log(text.toLowerCase()); // "hello"
Boolean
True or false values:
let isActive = true;
let isEmpty = false;
// Truthiness
if (1) console.log("1 is truthy");
if ("") console.log("empty string is falsy");
if (0) console.log("0 is falsy");
null and undefined
Special values representing "no value":
let x = null; // Intentionally empty
let y = undefined; // Variable declared but not assigned
let z; // Also undefined
Reference Data Types
Arrays
Ordered collections of values:
let fruits = ["Apple", "Banana", "Orange"];
let numbers = [1, 2, 3, 4, 5];
let mixed = [1, "two", true, null];
// Access elements (0-indexed)
console.log(fruits[0]); // "Apple"
console.log(fruits.length); // 3
// Modify arrays
fruits[0] = "Avocado"; // Change element
fruits.push("Grape"); // Add to end
fruits.pop(); // Remove last element
let num = 42;
let str = String(num); // "42"
let str2 = num.toString(); // "42"
Number Conversion
let str = "42";
let num = Number(str); // 42
let num2 = parseInt(str); // 42
let num3 = parseFloat("3.14"); // 3.14
Boolean Conversion
let bool = Boolean(1); // true
let bool2 = Boolean(0); // false
let bool3 = Boolean(""); // false
String Template Literals
Use backticks to create strings with interpolation:
let name = "Alice";
let age = 28;
// Without template literals
let message = "My name is " + name + " and I am " + age + " years old";
// With template literals
let message2 = `My name is ${name} and I am ${age} years old`;