⚙️ Lesson 14: Variables & Data Types

Declaring Variables

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

// 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

Objects

Collections of key-value pairs:

let person = { name: "Alice", age: 28, city: "New York", isActive: true }; // Access properties console.log(person.name); // "Alice" console.log(person["age"]); // 28 // Modify properties person.age = 29; person.email = "alice@example.com"; // Add new property // Delete property delete person.city;

Type Conversion

Converting between data types:

String Conversion

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`;

Type Checking

Use typeof to check data types:

Value typeof Result
42 "number"
"hello" "string"
true "boolean"
[1, 2, 3] "object"
{name: "Alice"} "object"
undefined "undefined"
null "object"

Common String Methods

Method Purpose Example
length Number of characters "hello".length → 5
toUpperCase() Convert to uppercase "hello".toUpperCase() → "HELLO"
toLowerCase() Convert to lowercase "HELLO".toLowerCase() → "hello"
substring() Extract part of string "hello".substring(0, 3) → "hel"
includes() Check if contains text "hello".includes("ell") → true
replace() Replace text "hello".replace("l", "x") → "hexlo"
split() Split into array "a,b,c".split(",") → ["a","b","c"]

Key Takeaways

← Back to Course | ← Previous | Next: Operators & Logic →