JavaScript ek powerful programming language hai jo mainly web pages ko interactive banane ke liye use hoti hai. Is article mein hum JavaScript ke basic topics samjhenge — like syntax, variables, data types, operators, comments, aur keywords.
2.1 JavaScript Syntax
JavaScript syntax ka matlab hota hai — code likhne ke rules.
Ek simple JavaScript code kuch aisa dikhta hai:
console.log("Hello, JavaScript!");
Explanation:
console.log()
ek built-in function hai jo output ko browser ke console mein print karta hai.- Statements usually semicolon (
;
) se end hoti hain. - JavaScript case-sensitive language hai —
Name
aurname
alag variables hote hain.
Note: JavaScript code likhne ke liye <script>
tag ka use hota hai, jab javascript HTML file mein use karte hai.
<script>
alert("Welcome to JS!");
</script>
2.2 Variables (var, let, const)
Variables ko ek container samajh sakte hai jisme hum data store karte hain. JavaScript mein variables declare karne ke teen tareeke hote hain:
1. var
- Ye old way hai variable declare karne ka.
- Function-scoped hota hai.
- Re-declare aur re-assign dono kar sakte ho.
var name = "Shakib";
name = "Ansari"; // Re-assigned
2. let
- Introduced in ES6.
- Block-scoped hota hai.
- Re-assign kar sakte ho, lekin re-declare nahi.
let age = 21;
age = 22; // Allowed
// let age = 23; Not allowed
3. const
- Constant variable declare karta hai.
- Na re-assign hota hai, na re-declare.
- Value fix rehti hai.
const pi = 3.14;
// pi = 3.15 Error
2.3 Data Types
JavaScript ek dynamically typed language hai, matlab variable ka type automatically decide hota hai.
Yahan 6 basic (primitive) data types hote hain:
- String
"Hello"
: Text data store karta hai. - Number
42
,3.14
: Numeric values. - Boolean
true
,false
: True/False values. - Null
null
: Empty value. - Undefined
let x;
: Variable declared but not assigned. - Symbol
Symbol("id")
: Unique identifier (advanced use).
Example:
let name = "Shakib"; // String
let age = 21; // Number
let isStudent = true; // Boolean
let car = null; // Null
let address; // Undefined
let id = Symbol("123"); // Symbol
2.4 Operators
Operators ka use values ko manipulate karne ke liye hota hai.
1. Arithmetic Operators
Used for mathematical calculations:
+, -, *, /, %, ++, --
Example:
let a = 10;
let b = 5;
console.log(a + b); // 15
2. Assignment Operators
Used to assign values:
=, +=, -=, *=, /=, %=
Example:
let x = 10;
x += 5; // x = x + 5 → 15
3. Comparison Operators
Used to compare values:
==, ===, !=, !==, >, <, >=, <=
Example:
console.log(5 == "5"); // true
console.log(5 === "5"); // false (ye value ka type bhi compare karta hai)
4. Logical Operators
Used to combine conditions:
&& (AND), || (OR), ! (NOT)
Example:
console.log(true && false); // false
5. Ternary Operator
Short form of if-else:
Example:
let result = (age >= 18) ? "Adult" : "Minor";
2.5 Comments in JavaScript
Comments ka use code explain karne ya temporarily disable karne ke liye hota hai.
a. Single-line comment:
// This is a single-line comment
b. Multi-line comment:
/*
This is
a multi-line comment
*/
Browser comments ko ignore karta hai, ye sirf developers ke liye helpful hoti hain.
2.6 Keywords in JavaScript
Keywords wo words hote hain jinki javascript mein ek special meaning hoti hai. Jo words keyword hote hai unhe variable name ke liye use nahi kar sakte.
Examples of keywords:
var, let, const, if, else, for, while, function, return, break, continue, switch, case, new, try, catch, throw, class, import, export
Example:
let for = 10; // Error (for is a keyword)
Conclusion
Is article mein humne dekha:
- JavaScript syntax kaise likhte hain.
- Variables (var, let, const) ke difference.
- Different data types.
- Common operators.
- Comments aur keywords ka use.
Comments