Master JavaScript from Zero to Hero
Learn JavaScript fundamentals with interactive examples, real-world projects, and hands-on exercises. Perfect for beginners and aspiring developers.
Why Learn JavaScript?
Web Development
JavaScript powers interactive websites and web applications. It's essential for frontend development.
In Demand Skill
JavaScript is one of the most popular programming languages. High demand = better job opportunities.
Versatile & Powerful
Use JavaScript for frontend, backend (Node.js), mobile apps, and even machine learning.
Easy to Learn
JavaScript has a gentle learning curve. Start building projects quickly, even as a beginner.
Your Learning Roadmap
Introduction & History
Understand what JavaScript is and how it evolved. Learn why it's essential for web development.
JavaScript Basics
Master variables, comments, and output methods. Learn how to write your first JavaScript code.
Data Types & Values
Explore strings, numbers, booleans, arrays, and objects. Understand how JavaScript stores data.
Operators
Learn arithmetic, comparison, logical, and assignment operators. Perform calculations and logic.
Math & Methods
Discover JavaScript's built-in Math object for calculations and random operations.
Practice & Projects
Build real projects, solve exercises, and test your knowledge with quizzes.
JavaScript History
The Birth of JavaScript (1995)
JavaScript was created in 1995 by Brendan Eich while he was working at Netscape Communications Corporation. Originally called "Mocha" and then "LiveScript," it was renamed to "JavaScript" as part of a marketing partnership between Netscape and Sun Microsystems.
Eich designed the language in just 10 days, which is remarkable given how widely used it is today!
Why "Java"Script?
The name "JavaScript" was a marketing decision to capitalize on the popularity of Java at the time. Despite the similar names, JavaScript and Java are completely different languages with different purposes and designs.
- JavaScript: Lightweight, interpreted, designed for browsers
- Java: Compiled, statically typed, more heavyweight
Evolution of JavaScript
ES3 (1999) - Standardization
JavaScript was standardized as ECMAScript 3. This brought consistency across different browsers and became the standard for the next decade.
ES5 (2009) - Modern JavaScript
ES5 introduced major features like:
- Strict mode for safer coding
- JSON support
- Array methods (forEach, map, filter, reduce)
- Object.create() for better inheritance
ES6/ES2015 - The Big Revolution
ES2015 was a massive update that transformed JavaScript:
- let and const: Block-scoped variables
- Arrow functions: Cleaner function syntax
- Classes: Object-oriented syntax
- Template literals: String interpolation with backticks
- Promises: Better async handling
- Modules: Import/export system
ES2016 onwards - Continuous Evolution
JavaScript continues to evolve with yearly updates adding new features:
- ES2017: async/await for cleaner asynchronous code
- ES2018: Rest/spread operators, finally blocks
- ES2019: Optional chaining, nullish coalescing
- ES2020+: BigInt, dynamic imports, and more
JavaScript Today
JavaScript is everywhere!
- Frontend: React, Vue, Angular - building dynamic UIs
- Backend: Node.js - server-side JavaScript
- Mobile: React Native, Flutter - cross-platform apps
- Desktop: Electron - desktop applications
- IoT: Arduino, Raspberry Pi programming
JavaScript Basics
Variables
Variables are containers for storing data values. JavaScript has three ways to declare variables: var, let, and const.
var (Legacy)
var name = "John";
var age = 25;
var isStudent = true;
Note: var is function-scoped and can be redeclared. It's mostly avoided in modern JavaScript.
let (Recommended)
let firstName = "Alice";
let score = 95;
let isActive = true;
let is block-scoped and cannot be redeclared. It's the modern way to declare variables.
const (For Constants)
const PI = 3.14159;
const MAX_SIZE = 100;
const userName = "Bob";
const declares a constant variable that cannot be reassigned. Use const by default!
Try It Yourself
Comments
Comments are notes in your code that JavaScript ignores. They help explain what your code does.
Single-line Comments
// This is a single-line comment
const x = 5; // Variable declaration
Multi-line Comments
/*
* This is a multi-line comment
* It can span across several lines
* Useful for explaining complex code sections
*/
Output Methods
JavaScript provides several ways to display output:
console.log() - Developer Console
console.log("Hello, World!");
console.log("The answer is", 42);
Best for debugging - visible in the browser's Developer Tools (press F12).
alert() - Pop-up Window
alert("Welcome to JavaScript!");
Shows a pop-up dialog box. The user must click OK to continue.
document.write() - Write to HTML
document.write("Hello!");
Writes directly to the HTML page. Rarely used in modern JavaScript.
innerHTML - Update HTML Elements
document.getElementById("demo").innerHTML = "Hello!";
Most modern way - updates the content of HTML elements.
Try It Yourself
JavaScript Data Types
JavaScript has 8 different data types. Variables can hold values of these different types.
Strings
A string is text enclosed in quotes. Can use single or double quotes.
const name = "Alice";
const message = 'Hello, World!';
const template = `Hello, ${name}!`;
Numbers
Numbers can be integers or decimals. JavaScript treats them as the same type.
const age = 25;
const price = 19.99;
const negative = -42;
const scientific = 2.5e2; // 250
Booleans
A boolean is either true or false. Used for conditions.
const isStudent = true;
const hasGraduated = false;
const result = 10 > 5; // true
Arrays
An array is an ordered collection of values.
const colors = ["red", "green", "blue"];
const numbers = [1, 2, 3, 4, 5];
const mixed = [42, "hello", true];
Objects
An object is a collection of key-value pairs.
const person = {
name: "Bob",
age: 30,
city: "New York",
isActive: true
};
Special Values
null
null represents "no value" or "nothing". It must be explicitly assigned.
const empty = null;
undefined
undefined means a variable has been declared but has not been assigned a value.
let x;
console.log(x); // undefined
BigInt
BigInt is for very large numbers beyond JavaScript's normal number limit.
const bigNumber = 9007199254740992n;
const anotherBig = BigInt("12345678901234567890");
Interactive Data Type Explorer
JavaScript Operators
Arithmetic Operators
Used to perform mathematical calculations.
Addition (+)
const result = 10 + 5; // 15
Subtraction (-)
const result = 10 - 3; // 7
Multiplication (*)
const result = 4 * 5; // 20
Division (/)
const result = 20 / 4; // 5
Modulus (%) - Remainder
const result = 17 % 5; // 2
Exponentiation (**)
const result = 2 ** 3; // 8 (2^3)
Assignment Operators
Used to assign values to variables.
Basic Assignment (=)
let x = 10;
Add and Assign (+=)
let x = 10;
x += 5; // Same as x = x + 5; Result: 15
Other Assignment Operators
x -= 3; // x = x - 3
x *= 2; // x = x * 2
x /= 4; // x = x / 4
x %= 5; // x = x % 5
Comparison Operators
Used to compare two values. Returns true or false.
Equal to (== or ===)
5 == "5" // true (loose comparison)
5 === "5" // false (strict comparison)
Not Equal to (!= or !==)
5 != "5" // false
5 !== "5" // true
Greater than (>)
10 > 5 // true
3 > 8 // false
Less than (<)
5 < 10 // true
8 < 3 // false
Greater than or equal to (>=)
10 >= 10 // true
5 >= 10 // false
Less than or equal to (<=)
10 <= 10 // true
15 <= 10 // false
Logical Operators
Used to combine or modify conditions.
AND (&&) - Both must be true
(5 > 3) && (10 > 8) // true
(5 > 3) && (10 < 8) // false
OR (||) - At least one must be true
(5 > 3) || (10 < 8) // true
(5 < 3) || (10 < 8) // false
NOT (!) - Reverses the result
!true // false
!false // true
!(5 > 3) // false
Operator Calculator
JavaScript Bitwise Operators
Bitwise operators work on binary representations of integers. They manipulate individual bits, making them useful for low-level operations, flags, and performance-critical code.
Understanding Binary
Before learning bitwise operators, it's helpful to understand how numbers are represented in binary:
5 in decimal = 101 in binary
3 in decimal = 11 in binary
12 in decimal = 1100 in binary
Bitwise AND (&)
Compares each bit of two numbers. Returns 1 only if both bits are 1.
const a = 5; // 0101 in binary
const b = 3; // 0011 in binary
const result = a & b; // 0001 = 1
Truth table: 1 & 1 = 1, 1 & 0 = 0, 0 & 1 = 0, 0 & 0 = 0
Bitwise OR (|)
Compares each bit of two numbers. Returns 1 if at least one bit is 1.
const a = 5; // 0101 in binary
const b = 3; // 0011 in binary
const result = a | b; // 0111 = 7
Truth table: 1 | 1 = 1, 1 | 0 = 1, 0 | 1 = 1, 0 | 0 = 0
Bitwise XOR (^)
Exclusive OR. Returns 1 if bits are different, 0 if they're the same.
const a = 5; // 0101 in binary
const b = 3; // 0011 in binary
const result = a ^ b; // 0110 = 6
Truth table: 1 ^ 1 = 0, 1 ^ 0 = 1, 0 ^ 1 = 1, 0 ^ 0 = 0
Bitwise NOT (~)
Inverts all bits. Converts the operand to a 32-bit integer and flips all bits.
const a = 5; // 0101
const result = ~a; // Result: -6
// In 32-bit: 5 becomes all 1s except for positions that were 1
~5 = -6
~0 = -1
Formula: ~x = -(x + 1)
Left Shift (<<)
Shifts all bits to the left by the specified number of positions. Fills with 0 on the right.
const a = 5; // 0101
const result = a << 1; // 1010 = 10
const result2 = a << 2; // 10100 = 20
// Left shift by n multiplies by 2^n
Effect: Each left shift multiplies the number by 2
Right Shift (>>)
Shifts all bits to the right by the specified number of positions. Preserves the sign bit.
const a = 10; // 1010
const result = a >> 1; // 0101 = 5
const result2 = a >> 2; // 0010 = 2
// Right shift by n divides by 2^n
Effect: Each right shift divides the number by 2 (floor division)
Unsigned Right Shift (>>>)
Shifts all bits to the right. Always fills with 0 on the left (ignores sign bit).
const a = -10;
const result1 = a >> 2; // -3 (preserves sign)
const result2 = a >>> 2; // 1073741821 (unsigned)
Difference from >>: Always treats the number as unsigned
Real-World Applications
1. Checking if a number is even or odd
const num = 7;
const isOdd = num & 1; // 1 if odd, 0 if even
2. Multiplying by powers of 2
const num = 5;
const times4 = num << 2; // 20 (faster than 5 * 4)
3. Setting Flags
const READ = 1; // 001
const WRITE = 2; // 010
const EXECUTE = 4; // 100
const permissions = READ | WRITE; // 011 (both flags set)
// Check if READ flag is set
const canRead = (permissions & READ) === READ;
4. Swapping two numbers without a temp variable
let a = 5;
let b = 3;
a = a ^ b;
b = a ^ b;
a = a ^ b;
console.log(a, b); // 3, 5
Bitwise Operators Playground
Number 1: 101
Number 2: 11
Result: -
Check if Number is Even or Odd
JavaScript Math Object
The Math object provides mathematical constants and functions for calculations.
Math.random()
Returns a random decimal number between 0 (inclusive) and 1 (exclusive).
const randomNum = Math.random();
// Returns something like: 0.7263749230742839
Math.floor()
Rounds down to the nearest integer.
Math.floor(4.7) // 4
Math.floor(4.2) // 4
Math.floor(-4.7) // -5
Math.ceil()
Rounds up to the nearest integer.
Math.ceil(4.2) // 5
Math.ceil(4.7) // 5
Math.ceil(-4.2) // -4
Math.round()
Rounds to the nearest integer (.5 rounds up).
Math.round(4.4) // 4
Math.round(4.5) // 5
Math.round(4.7) // 5
Math.max()
Returns the largest value from a list of numbers.
Math.max(5, 10, 3, 20) // 20
Math.max(-5, -10) // -5
Math.min()
Returns the smallest value from a list of numbers.
Math.min(5, 10, 3, 20) // 3
Math.min(-5, -10) // -10
Math.pow()
Returns the base to the power of the exponent.
Math.pow(2, 3) // 8 (2^3)
Math.pow(5, 2) // 25 (5^2)
2 ** 3 // 8 (modern way)
Math.sqrt()
Returns the square root of a number.
Math.sqrt(16) // 4
Math.sqrt(25) // 5
Math.sqrt(2) // 1.414...
Math Constants
Math.PI // 3.14159265359
Math.E // 2.718281828459
Math.SQRT2 // 1.41421356237
Math.LN2 // 0.693147180560
Math Methods Playground
Random Number Generator
Practice Exercises
Test your knowledge with these beginner exercises. Click "Show Solution" to see the answer.
Exercise 1: Variables & Output
Task: Create variables for your name and age, then display them using console.log().
Hint: Use let or const to create variables.
Exercise 2: Arithmetic Operators
Task: Calculate the sum, product, and quotient of two numbers: 50 and 10.
Exercise 3: Comparison Operators
Task: Compare two numbers (15 and 20) and check: is 15 greater than 20? Is 15 equal to 15?
Exercise 4: String Operations
Task: Create two string variables with your first and last name. Combine them and display the full name.
Exercise 5: Arrays
Task: Create an array of 5 numbers and find the largest number using Math.max().
Exercise 6: Math Object
Task: Generate a random number between 1 and 10 using Math.random() and Math.floor().
JavaScript Quiz
Test your knowledge! Select the correct answer for each question.
Interactive Projects
๐ฎ Number Guessing Game
I'm thinking of a number between 1 and 100. Try to guess it!
๐ฒ Random Dice Roller
Roll two dice and see the results!
๐ Age Calculator
Calculate your age based on your birth year.
๐งฎ Simple Calculator
A basic calculator for everyday math.
๐ก๏ธ Temperature Converter
Convert between Celsius and Fahrenheit.
โ๏ธ BMI Calculator
Calculate your Body Mass Index.