Now that you have your development environment set up and understand the basics of how JavaScript runs, it's time to dive into the fundamental building blocks of the language: its core syntax and the various types of data it can handle. Understanding these concepts is crucial for writing any meaningful JavaScript code.

2.1. Statements, Expressions, and Comments

Before we get into variables, let's clarify some basic terminology.

2.2. Variables: var, let, const

Variables are containers for storing data values. In JavaScript, you declare variables using keywords. Historically, var was the only way, but with ES6 (ECMAScript 2015), let and const were introduced, offering better ways to manage variable scope and mutability.

var (Legacy Declaration)

let (Modern Declaration)

const (Constant Declaration)

2.3. Data Types: Primitives and Non-primitives

JavaScript variables can hold many different types of data. These types are broadly categorized into Primitive and Non-Primitive (or Object) types.

Primitive Data Types

Primitive values are immutable (cannot be changed after creation) and are passed by value.

  1. String: Represents textual data. Enclosed in single quotes (' '), double quotes (" "), or backticks (` for template literals, which we'll cover in ES6+ features).
    let greeting = "Hello, world!";
    let name = 'Alice';
    let message = `You are ${name}.`; // Template literal
  2. Number: Represents both integer and floating-point numbers.
    let age = 30; // Integer
    let price = 99.99; // Floating-point
    let temperature = -5;
    let bigNumber = 1e6; // 1 * 10^6 = 1000000
    
    // Special Number values:
    let result = 0 / 0; // NaN (Not a Number)
    let infinity = 1 / 0; // Infinity
  3. Boolean: Represents a logical entity and can have only two values: true or false.
    let isActive = true;
    let hasPermission = false;
  4. null: Represents the intentional absence of any object value. It's a primitive value.
    let user = null; // Variable is explicitly empty
  5. undefined: Represents a variable that has been declared but has not yet been assigned a value.
    let quantity; // quantity is undefined
    console.log(quantity); // Output: undefined
  6. Symbol (ES6): Represents a unique identifier. Often used as keys for object properties to avoid name clashes.
    const id1 = Symbol('id');
    const id2 = Symbol('id');
    console.log(id1 === id2); // Output: false (each Symbol is unique)
  7. BigInt (ES11): Represents whole numbers larger than 2^53 - 1 (the largest number Number can reliably represent). You add n to the end of an integer to make it a BigInt.
    const veryBigNumber = 9007199254740991n;
    const anotherBigInt = BigInt(12345678901234567890);

Non-Primitive Data Type (Object)

Non-primitive values are mutable and are passed by reference.

  1. Object: The most complex data type. Everything else in JavaScript (arrays, functions, dates, regular expressions) is technically an object or behaves like one. Objects are collections of key-value pairs (properties).
    // An object literal
    let person = {
        firstName: "John",
        lastName: "Doe",
        age: 30,
        isStudent: false,
        hobbies: ["reading", "hiking"] // An array inside an object
    };
    
    // An array (which is a special type of object)
    let colors = ["red", "green", "blue"];
    
    // A function (which is also a special type of object)
    function greet() {
        console.log("Hello!");
    }

    We will dive much deeper into objects and arrays in Chapter 7.

2.4. Type Coercion and Type Checking

JavaScript is a dynamically typed language, meaning you don't declare the type of a variable explicitly, and a variable can hold different types of values over its lifetime. This flexibility can sometimes lead to unexpected behavior due to type coercion.

Type Coercion

Type coercion is JavaScript's automatic conversion of values from one data type to another. This happens implicitly (automatically by JavaScript) or explicitly (when you intentionally convert types).

Type Checking (typeof and instanceof)

To determine the type of a variable or value, you can use the typeof operator or, for objects, the instanceof operator.

Understanding variables and data types is foundational. In the next chapter, we'll learn how to perform operations on these data types using various operators.