learn2kode.in

Introduction to JavaScript

JavaScript is one of the core technologies of the web, alongside HTML and CSS. While HTML structures a webpage and CSS styles it, JavaScript adds interactivity and logic.
With JavaScript, you can:
JavaScript runs directly in the browser, making it fast and widely supported across all modern devices.

Why Learn JavaScript?

JavaScript is essential for anyone who wants to become a frontend, backend, or full-stack developer.
Key benefits:

How JavaScript Works

JavaScript code is executed by the browser’s JavaScript engine (like V8 in Chrome).
Basic flow:
JavaScript can:

JavaScript Syntax Basics

JavaScript syntax defines how code is written and executed.
Example
console.log("Hello, World!");
Key rules:

Variables in JavaScript

Variables store data values.
JavaScript uses:
Example
let name = "John";
const age = 25;

Data Types in JavaScript

JavaScript supports different types of data:

JavaScript is dynamically typed, meaning variable types can change.

Operators

Operators perform actions on values.
Common types

Comments in JavaScript

Comments explain code and are ignored by the browser.
Single-line comment
Multi-line comment
/*
  This is a multi-line comment
*/

Functions

Functions are reusable blocks of code.
Example
function greet(name) {
  return "Hello " + name;
}
Functions help

Conditionals (if, else, switch)

Conditionals execute code based on conditions.
Example
if (age >= 18) {
  console.log("Adult");
} else {
  console.log("Minor");
}
They help control program flow.

Loops

Loops repeat code multiple times.

Common loops:

Example
for (let i = 1; i <= 5; i++) {
  console.log(i);
}

Arrays

Arrays store multiple values in one variable.
Example
let colors = ["red", "blue", "green"];
Arrays are useful for lists, collections, and data handling.

Objects

Objects store data as key-value pairs.
Example
let user = {
  name: "Alice",
  age: 30
};
Objects represent real-world entities in code.

Strings

Strings represent text.
Example
let message = "Welcome to JavaScript";
JavaScript provides powerful string methods like:

Numbers

JavaScript handles both integers and decimals using the Number type.
Example
Built-in math functions allow rounding, random numbers, and calculations.

Dates

The Date object handles date and time.
Example
Useful for: