2 - Basics

Chapter 2: JavaScript Basics

Variables and Data Types

Variables in JavaScript

  • Variables: Containers for storing data values.

  • Declaration Keywords:

    • var: Older way to declare variables (function-scoped).

    • let: Block-scoped, used in modern JavaScript.

    • const: Block-scoped, cannot be reassigned (constant value).

Declaring Variables

  • Syntax:

    let name = "John";  // Using let
    const age = 25;      // Using const
    var country = "USA"; // Using var (not recommended)
  • Examples:

    let score = 100;
    score = 150;  // Reassigning with let
    const pi = 3.14;
    // pi = 3.15;  // Error: Assignment to constant variable
  • Best Practices:

    • Use let for variables that will change.

    • Use const for variables that should not change.

    • Avoid var unless dealing with legacy code.

Variable Scope

  • Global Scope: Variables declared outside any function have global scope.

    • Example:

  • Local Scope: Variables declared inside a function.

    • Example:

  • Block Scope: Variables declared with let or const inside {} are scoped to that block.

    • Example:

Data Types

Primitive Data Types

  • String: Text data, wrapped in quotes.

    • Example:

  • Number: Numeric values (integers and floats).

    • Example:

  • Boolean: Represents true or false.

    • Example:

  • Undefined: A variable that has been declared but not assigned a value.

    • Example:

  • Null: Represents an intentional absence of value.

    • Example:

  • Symbol: Unique identifier, used for more advanced operations.

    • Example:

  • BigInt: For very large integers beyond the Number type's limit.

    • Example:

Reference Data Types

  • Object: Collection of key-value pairs.

  • Array: List of items, can hold multiple data types.

  • Function: Block of code designed to perform a task, treated as a value.

  • Date: Represents a date and time.

Type Conversion

  • Implicit Conversion (Type Coercion): JavaScript automatically converts types.

    • Example:

  • Explicit Conversion: Manually converting types.

Basic Operators

Arithmetic Operators

  • Examples:

Assignment Operators

  • Examples:

Comparison Operators

  • Examples:

Logical Operators

  • Examples:

Control Structures

Conditional Statements

  • If-Else Statement: To execute code based on a condition.

  • Else If Statement: Handle multiple conditions.

  • Switch Statement: Useful for multiple conditions.

Loops

  • For Loop: Repeats code a certain number of times.

  • For...of Loop: Iterates over iterable objects (like arrays).

  • For...in Loop: Iterates over object properties.

  • While Loop: Repeats code while a condition is true.

  • Do-While Loop: Executes code at least once, then repeats while the condition is true.

Summary

  • Variables: Use let and const for modern JavaScript, understand scope.

  • Data Types: Understand primitive vs. reference types, type conversion.

  • Operators: Arithmetic, assignment, comparison, and logical operators.

  • Control Structures: Use if-else, switch, and loops (for, while, do-while) for program flow.


Next Chapter: Functions and Scope

Last updated