🌐 What is JavaScript and How It Works?
JavaScript is a high-level scripting language used to add functionality to web pages.
👉 It controls how a website behaves.
🔹 Example:
- Button click actions
- Form validations
- Showing alerts
- Animations
- Dynamic content updates
🧠 Where JavaScript is Used?
- 🌐 Websites (Frontend)
- ⚙️ Server-side apps (Node.js)
- 📱 Mobile apps (React Native)
- 🎮 Games and interactive apps
⚙️ How JavaScript Works?
JavaScript runs inside a web browser.
🔹 Step-by-step working:
- You write JavaScript code
Example:
console.log("Hello World");
2. Browser receives the code
When you open a webpage, the browser loads HTML, CSS, and JavaScript.
3. JavaScript engine executes it
Every browser has a JavaScript Engine:
Chrome → V8 engine
Firefox → SpiderMonkey
Safari → JavaScriptCore
4. Code is executed line by line
JavaScript is interpreted, so it runs step by step.
5. Output is shown
Example output:
Hello World
Simple Real-Life Example
👉 Think of a website like a car:
- HTML → Structure (body of car)
- CSS → Design (paint & look)
- JavaScript → Engine (makes it move)
⚡ Important Features of JavaScript
- Runs in browser
- Easy to learn
- Lightweight language
- Supports dynamic behavior
- Works with HTML & CSS
🎯 Example Program
let name = "Akalya";
console.log("Welcome " + name);
Output:
Welcome Akalya
🧠 Data Types in JavaScript
JavaScript has two main types of data:
🔹 1. Primitive Data Types
- Number → 10, 20
- String → "Hello"
- Boolean → true, false
- Undefined → variable declared but not assigned
- Null → empty value
Example:
let age = 20;
let name = "JS";
let isStudent = true;
🔹 2. Non-Primitive Data Types
- Array → list of values
- Object → collection of key-value pairs
Example:
let numbers = [1, 2, 3];
let person = {
name: "John",
age: 25
};
✍️ Variables in JavaScript
Variables are used to store data.
We can declare variables using:
🔹 var
var x = 10;
🔹 let
let y = 20;
🔹 const
const z = 30;
Difference Between var, let, const
var
- Old method
- Can redeclare
- Can change value
let
- Modern
- Cannot redeclare
- Can change value
const
- Constant
- Cannot redeclare
- Cannot change value
🎯 Simple Example
let name = "JavaScript";
const version = 2024;
console.log(name);
console.log(version);
Top comments (0)