Variables and Data Types in JS

In JavaScript, Variables are containers for storing data values. Think of them as labeled boxes where you can put information to use later in your program.

Variables and Datatypes in JavaScript

1. var, let, and const

Modern JavaScript uses three keywords to declare variables, each with different rules:

Keyword Scope Can Reassign? Description
varFunctionYesOld way (avoid using now).
letBlockYesModern way for values that change.
constBlockNoFor values that stay the same.

2. Primitive Data Types

JavaScript has several basic types of data:

  • String: Text like "Hello World".
  • Number: Integers or decimals like 25 or 3.14.
  • Boolean: Logical values: true or false.
  • Undefined: A variable that has been declared but not assigned a value.
  • Null: Represents an intentional "empty" value.

3. Non-Primitive: Objects & Arrays

While primitives hold single values, Objects and Arrays can hold collections of data.

// An Array (List)
let colors = ["purple", "white", "black"];

// An Object (Key-Value pairs)
let student = {
    name: "Alex",
    age: 21,
    isCoding: true
};

Knowledge Check

1. Which keyword should you use for a variable that will never change?
A) let | B) var | C) const

2. What is the data type of the value "123"?
A) Number | B) String | C) Boolean

3. Which of these represents an empty value intentionally?
A) Undefined | B) Null | C) Zero