Variables & Data Types
The basic building blocks of any JavaScript program.
Understanding Variables (var, let, const)
Variables are containers for storing data. Use let for variables that will change, and const for variables that won't. var is the older, function-scoped way.
// 'let' for variables that can change
let age = 30;
age = 31;
// 'const' for constant variables
const birthYear = 1995;
// birthYear = 1996; // This would cause an error!
// 'var' is the old way (generally avoid)
var oldWay = "Avoid this";
Common Data Types
JavaScript has "primitive" types (like strings, numbers, booleans) and "complex" types (like objects and arrays).
let x_str = "Hello"; // String
let x_num = 20.5; // Number
let x_bool = true; // Boolean
let x_obj = {name: "John"}; // Object
let x_arr = ["a", "b"]; // Array
let x_null = null; // null (intentional absence)
let x_undef; // undefined (unassigned)