JavaScript (JS) is a high-level, interpreted programming language used mainly to make web pages interactive and dynamic.

It works alongside:

  • HTML โ†’ to structure the web page
  • CSS โ†’ to style the web page
  • JavaScript โ†’ to add behavior and logic

Example:
๐Ÿ‘‰ HTML can display a button,
๐Ÿ‘‰ CSS can make it look nice,
๐Ÿ‘‰ JavaScript can make it do something when clicked.

๐Ÿง  Brief History
  • Created by Brendan Eich in 1995 for Netscape Navigator browser.
  • Originally called LiveScript, later renamed to JavaScript.
  • Now standardized as ECMAScript (ES) โ€” the latest versions are ES6, ES7, etc.
  • Runs in all modern web browsers (Chrome, Firefox, Safari, Edge).
โš™๏ธ How JavaScript Works

JavaScript is:

  • Client-side โ†’ runs directly in the userโ€™s browser (not on the server).
  • Interpreted โ†’ executes line by line, not compiled beforehand.
  • Event-driven โ†’ responds to user actions (like clicks or keypresses).

You can also run JavaScript outside browsers using Node.js.

๐Ÿ’ป Where to Write JavaScript

You can write JavaScript:

  1. Inside HTML files <script> alert("Hello, world!"); </script>
  2. In an external file <script src="script.js"></script> In script.js: alert("This is external JavaScript!");
๐Ÿ”ก Basic Syntax and Structure

JavaScript code is written as a series of statements, each ending with a semicolon (;).

Example:
let name = "Raj";
console.log("Hello " + name);
๐Ÿ”ข Variables

Variables are containers for storing data.

Declaring variables:
KeywordUseScope
varold, function-scopedโŒ not recommended
letmodern, block-scopedโœ… preferred
constfor constants (cannot be reassigned)โœ… preferred for fixed values
Example:
let age = 20;
const name = "Riya";

๐Ÿงฎ Data Types

JavaScript has primitive and non-primitive data types.

Primitive Types:
TypeExample
String"Hello"
Number42, 3.14
Booleantrue, false
Undefineddeclared but not assigned
Nullempty or no value
Symbolunique identifier
BigIntfor very large numbers
Example:
let name = "Rohan";  // string
let age = 18;        // number
let isStudent = true; // boolean
let city;             // undefined
โž• Operators

Used to perform operations on values.

TypeExamples
Arithmetic+ - * / % **
Assignment= += -= *= /=
Comparison== === != !== > < >= <=
Logical`&&
String+ (concatenation)

Example:

let x = 5, y = 10;
console.log(x + y); // 15
console.log(x > y); // false
๐Ÿ”„ Control Structures

Used to control program flow.

1. If-Else
let age = 18;
if (age >= 18) {
  console.log("You are an adult");
} else {
  console.log("You are a minor");
}
2. Switch
let day = 2;
switch(day) {
  case 1: console.log("Monday"); break;
  case 2: console.log("Tuesday"); break;
  default: console.log("Other day");
}
3. Loops
for(let i = 1; i <= 5; i++) {
  console.log(i);
}

let j = 1;
while(j <= 5) {
  console.log(j);
  j++;
}
๐Ÿงฉ Functions

Functions let you group reusable code.

Example:
function greet(name) {
  console.log("Hello " + name);
}

greet("Aarav"); // Output: Hello Aarav
Arrow Function (ES6+)
const greet = (name) => console.log(`Hello ${name}`);
๐Ÿ“ฆ Arrays

Arrays store multiple values in a single variable.

let fruits = ["Apple", "Banana", "Mango"];
console.log(fruits[1]); // Banana

fruits.push("Orange"); // add
fruits.pop();          // remove last
๐Ÿ“š Objects

Objects store data in key-value pairs.

let student = {
  name: "Riya",
  age: 21,
  course: "B.Tech"
};

console.log(student.name); // Riya
๐ŸŽ›๏ธ Events and the DOM

JavaScript can interact with HTML through the DOM (Document Object Model).

Example:

<button id="myBtn">Click Me</button>

<script>
  document.getElementById("myBtn").addEventListener("click", function() {
    alert("Button Clicked!");
  });
</script>
๐Ÿ“œ Comments

Use comments to describe code.

// This is a single-line comment

/*
This is
a multi-line comment
*/
โš™๏ธ JavaScript in the Browser

You can:

  • Log messages โ†’ console.log("message");
  • Show alerts โ†’ alert("Hi!");
  • Prompt user โ†’ prompt("Enter name:");
  • Confirm actions โ†’ confirm("Are you sure?");
๐Ÿš€ Modern JavaScript (ES6+ Features)
FeatureExample
Template Literals`Hello ${name}`
Let & Constlet, const
Arrow Functions() => {}
Destructuringconst {a,b} = obj;
Spread Operator[...arr1, ...arr2]
Classesclass Person {}
Modulesimport / export
๐Ÿงญ Advantages of JavaScript

โœ… Runs in all browsers
โœ… Easy to learn and flexible
โœ… Can be used for both front-end and back-end (via Node.js)
โœ… Large community and library support
โœ… Powers modern frameworks (React, Angular, Vue)

โš ๏ธ Limitations

โš ๏ธ Can be misused for insecure code (if not careful)
โš ๏ธ Different browsers may interpret JS slightly differently
โš ๏ธ Single-threaded (though async fixes this)

๐Ÿงฉ Example: Simple Interactive Web Page
<!DOCTYPE html>
<html>
<head>
  <title>JS Example</title>
</head>
<body>
  <h2 id="greeting">Hello!</h2>
  <button onclick="changeText()">Click Me</button>

  <script>
    function changeText() {
      document.getElementById("greeting").innerText = "You clicked the button!";
    }
  </script>
</body>
</html>

๐Ÿ‘‰ When you click the button, JavaScript updates the text dynamically.

๐Ÿ“˜ Summary
TopicDescription
Language TypeInterpreted, high-level scripting language
Used ForAdding interactivity to web pages
Runs InBrowser (and Node.js for server-side)
Key FeaturesDynamic typing, functions, objects, events
Related ToHTML (structure), CSS (style)

2 Comments

  1. Kavya agarwal

    Thanks for sharing this! Do you think this method works well with [related tech or framework]? Iโ€™m experimenting with it in my project.

  2. Manish Pabbodina

    This is outstanding work.

Leave a Reply

Your email address will not be published. Required fields are marked *