Skip to content

JavaScript Overview

JavaScript is one of the most widely used programming languages for web development. It enables developers to create interactive and dynamic websites by controlling HTML, CSS, and browser behavior.

Whether youโ€™re new to coding or looking to strengthen your JavaScript knowledge, this guide covers essential JavaScript fundamentals.


JavaScript (JS) is a lightweight, interpreted programming language that runs in web browsers. It is used to:

โœ… Create dynamic web pages (e.g., form validation, animations)
โœ… Handle events (e.g., button clicks, keyboard input)
โœ… Manipulate the DOM (Document Object Model)
โœ… Work with APIs and fetch data from servers


A simple โ€œHello, World!โ€ program in JavaScript looks like this:

console.log("Hello, World!");
  • console.log() โ†’ Prints output to the browser console.
  • โ€œHello, World!โ€ โ†’ A string displayed as output.

Variables store data values and can be declared using var, let, or const.

var name = "John"; // Old way (Avoid using var)
let age = 25; // Block-scoped, can be reassigned
const PI = 3.1416; // Constant, cannot be changed

โš ๏ธ Best Practice: Use let for variables that change, and const for fixed values. ๐Ÿ”น Data Types in JavaScript JavaScript has primitive and non-primitive data types:

  • String โ†’ โ€œHelloโ€
  • Number โ†’ 42, 3.14
  • Boolean โ†’ true, false
  • Undefined โ†’ A variable without a value
  • Null โ†’ Represents an empty or unknown value
  • Object โ†’ { key: value } pairs
  • Array โ†’ Ordered list of values [1, 2, 3]
  • Function โ†’ A reusable block of code
let person = { name: "John", age: 30 }; // Object
let colors = ["red", "green", "blue"]; // Array
function greet() { console.log("Hello!"); } // Function

Arithmetic Operators +, -, *, /, % (modulus), ** (exponentiation)

console.log(5 == "5"); // true (loose comparison)
console.log(5 === "5"); // false (strict comparison)

==, ===, !=, !==, >, <, >=, <=

console.log(5 == "5"); // true (loose comparison) console.log(5 === "5"); // false (strict comparison)

&& (AND), || (OR), ! (NOT)

let isAdult = (age >= 18) && (age < 60);
  1. Conditional Statements
let age = 20;
if (age >= 18) {
console.log("You are an adult.");
} else {
console.log("You are a minor.");
}
  1. Loops in JavaScript
  • For Loop
for (let i = 0; i < 5; i++) {
console.log("Iteration:", i);
}
  • While Loop
let count = 0;
while (count < 5) {
console.log("Count:", count);
count++;
}

Functions allow code reuse and improve maintainability.

Function Declaration

function greet(name) {
return "Hello, " + name + "!";
}
console.log(greet("Alice")); // Hello, Alice!
const add = (a, b) => a + b;
console.log(add(5, 3)); // 8

The Document Object Model (DOM) allows JavaScript to manipulate HTML elements dynamically.

document.getElementById("myButton").addEventListener("click", function() {
alert("Button clicked!");
});
  • document.getElementById(โ€œidโ€) โ€“ Select an element by ID
  • document.querySelector(โ€œ.classโ€) โ€“ Select the first matching element
  • element.innerHTML = โ€œNew Contentโ€ โ€“ Modify content
  • element.style.color = โ€œredโ€ โ€“ Change CSS properties

Modern JavaScript (ES6 and later) includes powerful features:

โœ… Template Literals

let name = "Alice";
console.log(`Hello, ${name}!`); // Hello, Alice!

โœ… Destructuring Assignment

let person = { name: "Bob", age: 25 };
let { name, age } = person;
console.log(name, age);

โœ… Spread Operator (โ€ฆ)

let numbers = [1, 2, 3];
let newNumbers = [...numbers, 4, 5];
console.log(newNumbers); // [1, 2, 3, 4, 5]

โœ… Async/Await (Handling Asynchronous Code)

async function fetchData() {
let response = await fetch("https://api.example.com/data");
let data = await response.json();
console.log(data);
}

JavaScript is an essential language for web development, powering interactive websites and modern applications. Understanding its fundamentalsโ€”from variables and functions to ES6+ featuresโ€”is the first step toward mastering frontend and full-stack development.

๐Ÿš€ Next Steps? Explore DOM Manipulation, JavaScript Frameworks (React, Angular, Vue), and Asynchronous JavaScript!