JavaScript 101: Learn Variables, Data Types, and Operators
Get Started with JavaScript Basics
Introduction: Why Learn JavaScript?
JavaScript isn’t just a language you learn in theory—it’s the backbone of dynamic web applications seen in e-commerce sites, social networks, dashboards, and more. JavaScript is everywhere. Imagine using Google Maps, scrolling through Instagram, or ordering food online—JavaScript makes all of this possible!
In this guide, we’ll break down JavaScript’s fundamentals using real-world examples and simple code snippets, ensuring you understand every concept with ease.
At its core, JavaScript lets you store, manipulate, and transform data. Whether it’s handling user information on a retail website or processing live data on a financial dashboard, JavaScript is everywhere. The fundamentals we cover include:
Variables: The containers that hold data.
Data Types: The kinds of values stored (numbers, strings, booleans, etc.).
Operators: Tools for performing calculations and comparisons.
Scope: The boundaries where your variables live.
1. Understanding Variables
What Are Variables?
Think of variables as labeled containers that store data. Imagine you own a coffee shop, and you need to track daily sales. You'd store this number in a variable. Variables might hold anything from user details to live data from an API.
JavaScript offers three main ways to declare variables:
var
– The traditional way, with function-scoped visibility.let
– Introduced in ES6, block-scoped and safer for many use cases.const
– Also block-scoped, used for values that should not change.
var :
The var keyword was the original way to declare variables in JavaScript.
It has function scope, meaning a variable declared with var is accessible anywhere within the function where it is defined.
However, it does not have block scope, which can lead to unexpected results in loops or conditionals.
Another important aspect of var is hoisting—JavaScript moves var declarations to the top of their scope, but they remain undefined until assigned a value.
let :
let provides block scope, meaning the variable is only accessible within the {} block where it is declared.
This makes it a safer alternative to var, especially when dealing with loops or conditionals.
Unlike var, let is hoisted but remains in a "temporal dead zone" until initialized, preventing accidental access before assignment.
Temporal Dead Zone: This is the period between when a variable is hoisted and when it is actually initialized with a value.
const :
The const keyword is also block-scoped, similar to let, but with one crucial difference—it cannot be reassigned after being declared.
This makes const ideal for storing values that should remain unchanged throughout execution.
However, if const holds an object or array, its properties or elements can still be modified, but the reference cannot be reassigned.
Like let, const is hoisted but remains uninitialized until assigned a value.
Declaring Variables :
// Using var (function-scoped)
var greeting = "Hello, world!";
console.log(greeting); // Output: Hello, world!
// Using let (block-scoped)
let age = 25;
console.log(age); // Output: 25
// Using const (block-scoped, constant value)
const PI = 3.14159;
console.log(PI); // Output: 3.14159
Example:
Imagine you’re building a social media platform. You might have variables that store user information such as their name, unique user ID, and online status:
// Using let and const as best practices
const userId = "USR102938"; // A unique identifier from your database
let userName = "Jane Doe"; // The user's display name
let isOnline = true; // A boolean indicating if the user is currently online
console.log(`User ${userName} (ID: ${userId}) is online: ${isOnline}`);
In an online shopping cart, variables are used to store product prices, discount rates, and totals:
// Product prices and discount
const priceOfItem = 49.99; // Price in dollars
const discountRate = 0.10; // 10% discount
let quantity = 3; // Number of items purchased
// Calculate total cost after discount
let totalCost = priceOfItem * quantity;
let discount = totalCost * discountRate;
totalCost = totalCost - discount;
console.log(`Total cost after discount: $${totalCost.toFixed(2)}`);
2. Understanding Data Types
Data types define the kind of values a variable can hold and how JavaScript interprets them. Think of it like organizing items in a warehouse—some shelves store numbers, others hold text, and some contain lists of items. JavaScript classifies these values into primitive and non-primitive (reference) types.
Primitive Data Types (Stored directly in memory)
A primitive data type is pre-defined by the programming language. They are immutable (cannot be changed once created) and store a single value.
String - A string represents a sequence of characters enclosed in single (
'
), double ("
), or backticks (``
) quotes. It is used to store text, like names, messages, or file paths.let name = "Alice"; let message = 'Hello, world!'; let template = `This is a template string`; console.log(name, message, template);
Number - Numeric data (e.g., price of coffee, number of orders)
let age = 25; // Integer let price = 99.99; // Floating-point console.log(age, price);
Boolean - Booleans hold only two values:
true
orfalse
. They are commonly used in conditional statements and logical operations.let isOnline = true; let hasAccess = false; console.log(isOnline, hasAccess);
Undefined - A variable that has been declared but not assigned a value is undefined.
let score; console.log(score); // Output: undefined
Null - null represents an intentional absence of a value. Unlike undefined, it is explicitly assigned by a programmer.
let userProfile = null; console.log(userProfile); // Output: null
Symbol (ES6+) - Symbol creates unique values, often used for object properties to prevent conflicts.
const id1 = Symbol("id"); const id2 = Symbol("id"); console.log(id1 === id2); // Output: false
Non-Primitive Data Types (Stored as references in memory)
Non-primitive data types, also known as reference types, are objects and derived data types. They store references to values rather than storing values directly. Mutable, allowing their values to be modified after assignment.
Objects
An object in JavaScript is a collection of key-value pairs, where each key (also called a property) is a string (or symbol) and each value can be any data type (primitive or non-primitive). Objects allow you to group related data and functionality together.
Here's how you might model aCar
object:
const car = {
brand: "Toyota",
model: "Camry",
year: 2020,
start: function() {
console.log("The car is starting...");
},
stop: function() {
console.log("The car is stopping...");
}
};
// Accessing object properties
console.log(car.brand); // Output: Toyota
console.log(car.year); // Output: 2020
// Calling a method
car.start(); // Output: The car is starting...
Arrays
An array is a special type of object used to store a collection of ordered elements. Each element in an array has an index, and in JavaScript, arrays can hold values of any data type, including other arrays or objects.
Here's how you might model a shopping list:
let shoppingList = ["Apples", "Bananas", "Oranges"];
console.log(shoppingList[0]); // Output: Apples
// Modifying an array element
shoppingList[1] = "Grapes";
console.log(shoppingList); // Output: ["Apples", "Grapes", "Oranges"]
// Adding an item to the array
shoppingList.push("Pineapple");
console.log(shoppingList); // Output: ["Apples", "Grapes", "Oranges", "Pineapple"]
Functions
Functions in JavaScript are first-class objects, which means they can be assigned to variables, passed as arguments to other functions, and returned from functions. Functions are used to define behavior and can be called to perform a task.
Here's a simple calculator function:
function add(a, b) {
return a + b;
}
function subtract(a, b) {
return a - b;
}
function multiply(a, b) {
return a * b;
}
console.log(add(5, 3)); // Output: 8
console.log(subtract(5, 3)); // Output: 2
console.log(multiply(5, 3)); // Output: 15
3. Type Conversion (Coercion)
JavaScript automatically converts types when needed.
Examples:
console.log("5" + 5); // Output: "55" (String)
console.log("5" - 1); // Output: 4 (Number)
To manually convert types:
let num = "42";
let converted = Number(num);
console.log(converted); // Output: 42
4. JavaScript Operators
Operators allow us to perform actions on data.
Types of Operators:
Arithmetic Operators (Perform Calculations)
let a = 10; let b = 4; console.log(a + b); // Output: 14 (Addition) console.log(a - b); // Output: 6 (Subtraction) console.log(a * b); // Output: 40 (Multiplication) console.log(a / b); // Output: 2.5 (Division) console.log(a % b); // Output: 2 (Modulus) console.log(a++); // Output: 10 (Post-increment), a becomes 11 after this console.log(--a); // Output: 10 (Pre-decrement)
Assignment Operators
javascriptCopyEditlet x = 5; x += 3; // x = x + 3 -> 8 console.log(x); // Output: 8 x -= 2; // x = x - 2 -> 6 console.log(x); // Output: 6 x *= 2; // x = x * 2 -> 12 console.log(x); // Output: 12 x /= 3; // x = x / 3 -> 4 console.log(x); // Output: 4 x %= 2; // x = x % 2 -> 0 console.log(x); // Output: 0
Comparison Operators (Compare Values)
console.log(5 == '5'); // Output: true (Loose comparison) console.log(5 === '5'); // Output: false (Strict comparison) console.log(10 > 5); // Output: true console.log(5 <= 3); // Output: false console.log(7 != 5); // Output: true console.log(7 !== '7'); // Output: true
Logical Operators (Work with Boolean Values)
let a = true; let b = false; console.log(a && b); // Output: false (Both must be true for AND) console.log(a || b); // Output: true (Either can be true for OR) console.log(!a); // Output: false (Negation)
Unary Operators
let a = 5; console.log(++a); // Output: 6 (Increment before use) console.log(--a); // Output: 5 (Decrement before use) let b = "5"; console.log(+b); // Output: 5 (Converts string to number) console.log(-b); // Output: -5 (Converts string to negative number)
Ternary Operator
let age = 18; let result = (age >= 18) ? "Adult" : "Minor"; console.log(result); // Output: "Adult"
Typeof Operator
console.log(typeof 5); // Output: "number" console.log(typeof "Hello"); // Output: "string" console.log(typeof true); // Output: "boolean" console.log(typeof undefined); // Output: "undefined"