Lesson 2: Variables, Data Types, and Operators

“Discover how JavaScript stores, categorizes, and manipulates data in memory.”

Lesson 2: Variables, Data Types, and Operators

What is a Variable?

A [variable] is a container for storing data values. In JavaScript, we use variables to store information that our program can reference, manipulate, or update later.

For comprehensive specifications, refer to the official ECMAScript Language Specification.

---

Variable Declarations: let, const, and var

Historically, JavaScript used the keyword var. In modern standards, we avoid var due to scope limitations and predictability issues. Instead, we use let and const.

* let: Used for variables that can be updated (reassigned) during runtime.
* const: Used for variables that must remain constant. They cannot be reassigned after declaration.

#### Code Example: Variable Declarations
// Declaring a mutable variable
let itemsInCart = 3;
itemsInCart = 4; // Reassignment is allowed
console.log("Items in cart:", itemsInCart);

// Declaring a constant variable
const taxRate = 0.08;
// taxRate = 0.10; // Uncaught TypeError: Assignment to constant variable.
console.log("Tax rate:", taxRate);

#### Visual Output Demonstration:
Items in cart: 4
Tax rate: 0.08

---

JavaScript Data Types

JavaScript is a [loosely typed] or dynamic language, meaning you do not have to declare the type of data a variable holds beforehand. The language automatically determines it at runtime.

There are two main categories of data types: Primitives and Reference Types (Objects).

  • #### Primitive Data Types:
  • Number: Integers and decimals (e.g., 10, 3.14).
  • String: Textual data enclosed in single quotes, double quotes, or backticks (e.g., 'Hello').
  • Boolean: Logic values, either true or false.
  • Undefined: A variable that has been declared but not assigned a value.
  • Null: Intentional absence of value.
  • Symbol: A unique, immutable value.
  • BigInt: Safe storage for extremely large numbers.

#### Code Example: Exploring Types
let userAge = 25; // Number
let userName = "Alice"; // String
let isSubscribed = true; // Boolean
let userAddress; // Undefined
let paymentDetails = null; // Null

console.log(typeof userAge);
console.log(typeof userName);
console.log(typeof isSubscribed);
console.log(typeof userAddress);
console.log(typeof paymentDetails); // Returns "object" (a legacy JS bug)

#### Visual Output Demonstration:
number
string
boolean
undefined
object

---

Basic Operators

Operators allow us to perform mathematical or logical calculations on our data.

#### Arithmetic Operators:
* + (Addition), - (Subtraction), * (Multiplication), / (Division), % (Modulus/Remainder).

#### Code Example: Basic Calculations
let price = 100;
let discount = 15;
let finalPrice = price - discount;
let tax = finalPrice * 0.08;
let total = finalPrice + tax;

console.log("Subtotal:", finalPrice);
console.log("Total with tax:", total);

#### Visual Output Demonstration:
Subtotal: 85
Total with tax: 91.8

---

Frequently Asked Questions (FAQs)

#### Q1: Should I ever use 'var' in modern programming?
* No. In contemporary standards, you should stick to let and const. var does not respect block scoping, which can lead to hard-to-find bugs and unexpected behaviors.

#### Q2: What is the difference between null and undefined?
* undefined means a variable has been declared but has not yet been assigned a value. null is an active assignment representing a completely empty or non-existent value.

#### Q3: What is a dynamic language?
* A dynamic language (like JavaScript) determines variable types automatically during program execution. This contrasts with static languages (like Java or C++), where you must explicitly define the type of variable (e.g., int number = 10;) before compile time.

Shanawar AliFounder and developer at S Pro Coder, sharing practical coding and technology guides.