
Got it π Letβs go through the basic knowledge of JavaScript step by step.
π What is JavaScript?
- JavaScript (JS) is a high-level, interpreted programming language mainly used to make web pages interactive and dynamic.
- It runs in the browser (client-side), but also on servers (using Node.js).
- Along with HTML (structure) and CSS (style), JavaScript is the third core technology of the web.
β¨ Key Features
- Lightweight & interpreted: No compilation needed, runs directly in the browser.
- Event-driven: Reacts to user actions like clicks, key presses, etc.
- Prototype-based OOP: Uses objects and prototypes instead of classical classes (though modern JS supports
class
). - Cross-platform: Runs in all major browsers and environments (Node.js).
π Basic Concepts
1. Variables
Used to store data.
let name = "Raj"; // Block-scoped, modern
var age = 20; // Function-scoped, older
const PI = 3.14; // Constant, cannot be reassigned
2. Data Types
- Primitive: string, number, boolean, undefined, null, symbol, bigint
- Non-primitive: object, array, function
let str = "Hello"; // string
let num = 42; // number
let isAlive = true; // boolean
let x; // undefined
let y = null; // null
let arr = [1, 2, 3]; // array
let obj = {name: "Raj"}; // object
3. Operators
- Arithmetic:
+ - * / % **
- Assignment:
= += -=
- Comparison:
== === != !== > < >= <=
- Logical:
&& || !
- String:
"Hello" + "World"
4. Functions
Reusable blocks of code.
function greet(name) {
return "Hello " + name;
}
console.log(greet("Raj"));
Arrow function (modern):
const add = (a, b) => a + b;
console.log(add(5, 3));
5. Control Statements
let age = 18;
if (age >= 18) {
console.log("Adult");
} else {
console.log("Minor");
}
for (let i = 0; i < 5; i++) {
console.log(i);
}
while (age < 21) {
console.log("Growing...");
age++;
}
6. Objects & Arrays
// Object
let person = {
name: "Raj",
age: 20
};
console.log(person.name);
// Array
let fruits = ["Apple", "Banana", "Mango"];
console.log(fruits[1]); // Banana
7. Events & DOM
JavaScript can manipulate HTML & CSS via DOM (Document Object Model).
<button onclick="showMessage()">Click Me</button>
<script>
function showMessage() {
alert("Hello, Raj!");
}
</script>
8. ES6+ Features (Modern JS)
- Template literals:
`Hello ${name}`
- Destructuring:
let [a, b] = [10, 20]; let {name, age} = person;
- Promises & Async/Await for handling asynchronous code.
- Modules (
import
/export
).