Notes – Introduction to JavaScript

What is JavaScript?

JavaScript (JS) is a high-level, interpreted programming language used to make web pages interactive. It is one of the core technologies of web development, along with HTML and CSS.


Key Features of JavaScript

  • Lightweight โ€“ Runs inside the browser without the need for extra software.
  • Interpreted โ€“ No need for compilation, the browser executes the code directly.
  • Event-driven โ€“ Can handle user interactions like clicks, form submissions, and key presses.
  • Dynamic โ€“ Allows modifying HTML and CSS in real-time.
  • Cross-platform โ€“ Works on all major browsers and operating systems.

Why is JavaScript Important?

JavaScript is used everywhere in modern web development. It helps create:

  • Dynamic websites โ€“ Interactive content, animations, and real-time updates.
  • Web applications โ€“ Forms, chats, notifications, and dashboards.
  • Mobile applications โ€“ With frameworks like React Native.
  • Server-side applications โ€“ Using Node.js.

How JavaScript Works in the Browser

  1. The browser downloads the web page (HTML, CSS, JS files).
  2. The JavaScript engine inside the browser reads and executes the JS code.
  3. JS can manipulate the webpage elements and handle user interactions.

JavaScript Execution in a Webpage

JavaScript can be written in two ways:

  1. Inline JavaScript (Inside HTML elements)

    <button onclick="alert('Hello, JavaScript!')">Click Me</button>
  2. External JavaScript File

    <script src="script.js"></script>

    This keeps the HTML clean and separates logic from structure.

Basic JavaScript Syntax

Here are a few simple JavaScript concepts:

ConceptExample
Variableslet name = "John";
Functionsfunction greet() { alert("Hello!"); }
Eventsdocument.getElementById("btn").onclick = greet;
Loopsfor(let i = 0; i < 5; i++) { console.log(i); }
Conditionsif (age > 18) { console.log("Adult"); }

How to Run JavaScript?

You can execute JavaScript using:

  1. Browser Console โ€“ Open Developer Tools (F12 in Chrome) โ†’ Console โ†’ Type JS Code.
  2. Inside an HTML file โ€“ Use <script> tags within an HTML document.
  3. External JS file โ€“ Write JavaScript in a .js file and link it to HTML.