JavaScript Complete Learning
Start your journey with JavaScript, the programming language of the Web.

Introduction to JavaScript#
JavaScript is a high-level, interpreted programming language that enables interactive web pages. It's an essential part of web applications and is one of the core technologies of the World Wide Web, alongside HTML and CSS. JavaScript is a multi-paradigm language supporting object-oriented, imperative, and functional programming styles.
History of JavaScript#
JavaScript was created by Brendan Eich in 1995 while working at Netscape. Originally named Mocha, it was later renamed to LiveScript and finally JavaScript. Despite the name similarity, JavaScript is not related to Java. ECMAScript is the standardized specification that JavaScript follows.
Where JavaScript Runs#
JavaScript can run in:
- Web Browsers - Client-side JavaScript
- Node.js - Server-side JavaScript
- Mobile Apps - React Native, Ionic
- Desktop Apps - Electron
Variables and Data Types#
JavaScript has three ways to declare variables:
// let - block-scoped, can be reassigned
let name = "John";
name = "Jane"; // OK
// const - block-scoped, cannot be reassigned
const age = 25;
// age = 26; // Error!
// var - function-scoped, can be reassigned (avoid in modern JS)
var city = "Moradabad";
// Data types
let string = "Hello World"; // String
let number = 42; // Number
let float = 3.14; // Number (no separate float type)
let boolean = true; // Boolean
let array = [1, 2, 3]; // Array
let object = { name: "John", age: 25 }; // Object
let nullValue = null; // Null
let undefinedValue = undefined; // Undefined
let symbol = Symbol("id"); // Symbol (ES6)
let bigInt = 9007199254740991n; // BigInt (ES2020)Operators#
// Arithmetic
let sum = 5 + 3; // 8
let diff = 5 - 3; // 2
let product = 5 * 3; // 15
let quotient = 5 / 3; // 1.666...
let remainder = 5 % 3; // 2
let power = 5 ** 3; // 125
// Comparison
5 == "5" // true (loose equality)
5 === "5" // false (strict equality)
5 != "5" // false
5 !== "5" // true
5 > 3 // true
5 < 3 // false
5 >= 5 // true
// Logical
true && false // false (AND)
true || false // true (OR)
!true // false (NOT)
// Assignment
let x = 5;
x += 3; // x = x + 3
x -= 2; // x = x - 2
x *= 2; // x = x * 2
x /= 2; // x = x / 2Functions#
Functions are reusable blocks of code:
// Function declaration
function greet(name) {
return `Hello, ${name}!`;
}
// Function expression
const multiply = function(a, b) {
return a * b;
};
// Arrow function (ES6)
const add = (a, b) => a + b;
// Arrow function with single parameter
const square = x => x * x;
// Arrow function with body
const greetUser = (name) => {
const message = `Hello, ${name}!`;
return message;
};
// Default parameters
function greet(name = "Guest") {
return `Hello, ${name}!`;
}
// Rest parameters
function sum(...numbers) {
return numbers.reduce((acc, n) => acc + n, 0);
}Control Structures#
// If-else
if (age >= 18) {
console.log("Adult");
} else if (age >= 13) {
console.log("Teenager");
} else {
console.log("Child");
}
// Ternary operator
const status = age >= 18 ? "Adult" : "Minor";
// Switch statement
switch (day) {
case "Monday":
console.log("Start of week");
break;
case "Friday":
console.log("Weekend coming!");
break;
default:
console.log("Regular day");
}
// For loop
for (let i = 0; i < 5; i++) {
console.log(i);
}
// For...of loop (arrays)
for (const item of array) {
console.log(item);
}
// For...in loop (objects)
for (const key in object) {
console.log(key, object[key]);
}
// While loop
while (count < 10) {
count++;
}
// Do-while loop
do {
count++;
} while (count < 10);Arrays#
// Creating arrays
const fruits = ["Apple", "Banana", "Orange"];
const numbers = new Array(1, 2, 3);
// Accessing elements
fruits[0]; // "Apple"
fruits.length; // 3
// Array methods
const numbers = [1, 2, 3, 4, 5];
// Map - transform each element
const doubled = numbers.map(n => n * 2); // [2, 4, 6, 8, 10]
// Filter - select elements
const evens = numbers.filter(n => n % 2 === 0); // [2, 4]
// Reduce - reduce to single value
const sum = numbers.reduce((acc, n) => acc + n, 0); // 15
// Find - find first matching element
const found = numbers.find(n => n > 3); // 4
// Some - check if any element matches
const hasEven = numbers.some(n => n % 2 === 0); // true
// Every - check if all elements match
const allPositive = numbers.every(n => n > 0); // true
// Push/Pop - add/remove from end
numbers.push(6); // [1, 2, 3, 4, 5, 6]
numbers.pop(); // [1, 2, 3, 4, 5]
// Shift/Unshift - add/remove from start
numbers.unshift(0); // [0, 1, 2, 3, 4, 5]
numbers.shift(); // [1, 2, 3, 4, 5]
// Slice - extract portion
const sliced = numbers.slice(1, 3); // [2, 3]
// Splice - add/remove elements
numbers.splice(1, 2, 10, 20); // [1, 10, 20, 4, 5]Objects#
// Creating objects
const person = {
name: "John",
age: 25,
city: "Moradabad"
};
// Accessing properties
person.name; // "John"
person["age"]; // 25
// Adding properties
person.email = "john@example.com";
// Object methods
const person = {
name: "John",
greet: function() {
return `Hello, I'm ${this.name}`;
},
// Shorthand method (ES6)
introduce() {
return `I'm ${this.name}`;
}
};
// Object destructuring (ES6)
const { name, age } = person;
// Spread operator
const newPerson = { ...person, country: "India" };
// Object.keys, Object.values, Object.entries
Object.keys(person); // ["name", "age", "city"]
Object.values(person); // ["John", 25, "Moradabad"]
Object.entries(person); // [["name", "John"], ["age", 25], ...]ES6+ Features#
// Template literals
const message = `Hello, ${name}! You are ${age} years old.`;
// Destructuring
const [first, second] = [1, 2];
const { name, age } = person;
// Spread operator
const newArray = [...oldArray, 4, 5];
const newObject = { ...oldObject, newProp: "value" };
// Arrow functions
const add = (a, b) => a + b;
// Classes
class Person {
constructor(name, age) {
this.name = name;
this.age = age;
}
greet() {
return `Hello, I'm ${this.name}`;
}
}
// Promises
const fetchData = () => {
return new Promise((resolve, reject) => {
// async operation
resolve(data);
});
};
// Async/Await
async function getData() {
try {
const data = await fetchData();
return data;
} catch (error) {
console.error(error);
}
}DOM Manipulation#
// Selecting elements
const element = document.getElementById("myId");
const elements = document.querySelectorAll(".myClass");
const firstElement = document.querySelector(".myClass");
// Modifying content
element.textContent = "New text";
element.innerHTML = "<strong>Bold text</strong>";
element.setAttribute("class", "new-class");
element.classList.add("active");
element.classList.remove("inactive");
element.classList.toggle("active");
// Creating elements
const newDiv = document.createElement("div");
newDiv.textContent = "New element";
document.body.appendChild(newDiv);
// Event listeners
element.addEventListener("click", () => {
console.log("Clicked!");
});
element.addEventListener("click", handleClick);
function handleClick(event) {
console.log("Clicked!", event);
}
// Event delegation
document.addEventListener("click", (e) => {
if (e.target.matches(".button")) {
console.log("Button clicked");
}
});Error Handling#
// Try-catch
try {
// code that might throw error
riskyOperation();
} catch (error) {
console.error("Error occurred:", error);
} finally {
// code that always runs
cleanup();
}
// Throwing errors
if (age < 0) {
throw new Error("Age cannot be negative");
}Best Practices#
- Use const by default, let when reassignment is needed
- Avoid var (use let/const instead)
- Use === instead of == for comparisons
- Use meaningful variable and function names
- Write small, focused functions
- Handle errors properly with try-catch
- Use modern ES6+ features
- Follow consistent code style
- Comment complex logic
- Use arrow functions for callbacks
Ready to test your knowledge?
Take the quiz to reinforce what you've learned in this article.
Start Quiz