JavaScript Learning Path
A comprehensive guide to mastering JavaScript from fundamentals to advanced concepts
Introduction to JavaScript
Learn what JavaScript is and why it's important for web development
JavaScript is a high-level, interpreted programming language that is one of the core technologies of the web. It allows you to add interactivity to websites, create web applications, and even build server-side applications.
Your First JavaScript Code
javascript
1// This is a comment
2console.log("Hello, World!"); // Prints to the console
34// You can also use alert to show a popup
5// alert("Hello from JavaScript!");
Pro Tip
You can open your browser's developer tools (F12 or Right-click → Inspect) to see console output.
Variables and Data Types
Learn how to store and manipulate data in JavaScript
Declaring Variables
JavaScript has three ways to declare variables: var
, let
, and const
.
javascript
1// var (older way, function scoped)
2var name = "John";
34// let (block scoped, can be reassigned)
5let age = 25;
6age = 26; // This is valid
78// const (block scoped, cannot be reassigned)
9const PI = 3.14159;
10// PI = 3.14; // This would cause an error
Data Types
JavaScript has several primitive data types and one complex data type (Object).
javascript
1// Primitive types
2let string = "Hello"; // String
3let number = 42; // Number
4let decimal = 3.14; // Also a Number
5let boolean = true; // Boolean
6let nullValue = null; // Null
7let undefinedValue; // Undefined
8let bigInt = 9007199254740991n; // BigInt
9let symbol = Symbol("unique"); // Symbol
1011// Check the type using typeof
12console.log(typeof string); // "string"
13console.log(typeof number); // "number"
14console.log(typeof boolean); // "boolean"
1516// Complex type: Object
17let person = {
18name: "Alice",
19age: 30,
20isStudent: false
21};
2223// Arrays are also objects
24let fruits = ["apple", "banana", "orange"];
2526console.log(typeof person); // "object"
27console.log(typeof fruits); // "object"
28console.log(Array.isArray(fruits)); // true
Operators and Expressions
Learn how to perform operations on values
Arithmetic Operators
javascript
1let a = 10;
2let b = 3;
34console.log(a + b); // Addition: 13
5console.log(a - b); // Subtraction: 7
6console.log(a * b); // Multiplication: 30
7console.log(a / b); // Division: 3.3333...
8console.log(a % b); // Modulus (remainder): 1
9console.log(a ** b); // Exponentiation: 1000 (10^3)
1011// Increment and decrement
12let c = 5;
13console.log(c++); // 5 (returns c, then increments)
14console.log(c); // 6
15console.log(++c); // 7 (increments c, then returns it)
Comparison Operators
javascript
1let x = 5;
2let y = "5";
34console.log(x == y); // true (loose equality, converts types)
5console.log(x === y); // false (strict equality, checks types)
6console.log(x != y); // false (loose inequality)
7console.log(x !== y); // true (strict inequality)
8console.log(x > 3); // true
9console.log(x <= 5); // true
Logical Operators
javascript
1let isAdult = true;
2let hasLicense = false;
34console.log(isAdult && hasLicense); // AND: false
5console.log(isAdult || hasLicense); // OR: true
6console.log(!isAdult); // NOT: false
78// Short-circuit evaluation
9let name = "";
10let defaultName = name || "Anonymous"; // "Anonymous"
1112let user = { name: "John" };
13let userName = user && user.name; // "John"
Control Flow
Learn how to control the flow of your program with conditionals and loops
Conditional Statements
javascript
1let age = 18;
23// if statement
4if (age >= 18) {
5console.log("You are an adult");
6}
78// if-else statement
9if (age >= 18) {
10console.log("You are an adult");
11} else {
12console.log("You are a minor");
13}
1415// if-else if-else statement
16if (age < 13) {
17console.log("Child");
18} else if (age < 18) {
19console.log("Teenager");
20} else {
21console.log("Adult");
22}
2324// Ternary operator (condition ? trueValue : falseValue)
25let status = age >= 18 ? "Adult" : "Minor";
26console.log(status);
2728// Switch statement
29let day = 2;
30switch (day) {
31case 0:
32console.log("Sunday");
33break;
34case 1:
35console.log("Monday");
36break;
37case 2:
38console.log("Tuesday");
39break;
40default:
41console.log("Another day");
42}
Loops
javascript
1// for loop
2for (let i = 0; i < 5; i++) {
3console.log("Iteration " + i);
4}
56// while loop
7let count = 0;
8while (count < 3) {
9console.log("Count: " + count);
10count++;
11}
1213// do-while loop (always executes at least once)
14let num = 0;
15do {
16console.log("Number: " + num);
17num++;
18} while (num < 3);
1920// for...of loop (iterates over iterable values)
21const colors = ["red", "green", "blue"];
22for (const color of colors) {
23console.log(color);
24}
2526// for...in loop (iterates over object properties)
27const person = { name: "John", age: 30, job: "developer" };
28for (const key in person) {
29console.log(key + ": " + person[key]);
30}