JavaScript is one of the most important programming languages in the world. It powers interactive websites, modern web applications, backend servers, mobile apps, desktop apps, and more. Whether you are getting started or leveling up your skills, understanding JavaScript deeply will open countless opportunities.
This guide walks you through the core concepts of JavaScript with clear explanations and practical code examples.
1. What Is JavaScript?
JavaScript (JS) is a high-level, dynamic, interpreted programming language primarily used to build interactive websites. It runs inside all modern browsers and can also run on servers using Node.js.
JS is:
- Beginner-friendly
- Extremely popular (the most used language on GitHub)
- Flexible (frontend, backend, mobile, desktop, embedded apps)
- Supported everywhere
2. Your First JavaScript Program
You can run JS in the browser console or inside a file.
Create a file named script.js:
console.log("Hello JavaScript!");
Run it in your browser by linking it in HTML:
<!DOCTYPE html>
<html>
<body>
<script src="script.js"></script>
</body>
</html>
3. Variables and Data Types
JavaScript uses three keywords to declare variables:
-
let– block-scoped, recommended -
const– cannot be reassigned -
var– old syntax (avoid using)
Example:
let name = "Farhad";
const age = 25;
var country = "Afghanistan";
console.log(name, age, country);
Major Data Types
- String
- Number
- Boolean
- Null
- Undefined
- Object
- Array
- Function
let str = "Hello";
let num = 42;
let isOnline = true;
let emptyValue = null;
let notAssigned;
let person = { name: "Klie", age: 22 };
let numbers = [1, 2, 3];
console.log(typeof str, typeof num, typeof isOnline);
4. Operators in JavaScript
Arithmetic Operators
let a = 10;
let b = 3;
console.log(a + b);
console.log(a - b);
console.log(a * b);
console.log(a / b);
console.log(a % b);
Comparison Operators
console.log(5 == "5"); // true
console.log(5 === "5"); // false
console.log(10 > 3); // true
5. Conditional Statements
let score = 85;
if (score > 90) {
console.log("Excellent");
} else if (score > 70) {
console.log("Good");
} else {
console.log("Try Again");
}
6. Functions in JavaScript
Function Declaration
function greet(name) {
return "Hello " + name;
}
console.log(greet("Farhad"));
Arrow Function
const add = (a, b) => a + b;
console.log(add(10, 20));
7. Arrays and Array Methods
let fruits = ["Apple", "Banana", "Orange"];
fruits.push("Mango"); // Add
fruits.pop(); // Remove last
console.log(fruits.includes("Banana"));
console.log(fruits);
Useful Methods
.map().filter().reduce().forEach()
Example:
let numbers = [1, 2, 3, 4, 5];
let doubled = numbers.map(n => n * 2);
let evens = numbers.filter(n => n % 2 === 0);
console.log(doubled);
console.log(evens);
8. Objects & Object Methods
Objects store data in key-value pairs.
const user = {
name: "Farhad",
age: 22,
greet() {
console.log("Welcome " + this.name);
},
};
user.greet();
9. Loops in JavaScript
For loop:
for (let i = 1; i <= 5; i++) {
console.log("Number: " + i);
}
For…of loop (arrays):
let colors = ["Red", "Green", "Blue"];
for (let color of colors) {
console.log(color);
}
While loop:
let i = 1;
while (i <= 3) {
console.log(i);
i++;
}
10. DOM Manipulation (Browser JavaScript)
DOM = Document Object Model, used to interact with HTML.
Example:
<h1 id="title">Old Title</h1>
<button onclick="changeTitle()">Click Me</button>
<script>
function changeTitle() {
document.getElementById("title").textContent = "New Title Updated!";
}
</script>
11. Events in JavaScript
<button id="btn">Click Me</button>
<script>
document.getElementById("btn").addEventListener("click", () => {
alert("Button clicked");
});
</script>
12. Intro to Asynchronous JavaScript
JavaScript handles async operations using:
- Callbacks
- Promises
- async/await
Promise Example:
function fetchData() {
return new Promise(resolve => {
setTimeout(() => resolve("Data loaded"), 2000);
});
}
fetchData().then(result => console.log(result));
async/await Example:
async function loadData() {
const data = await fetchData();
console.log(data);
}
loadData();
13. Fetch API (Basic Networking)
fetch("https://jsonplaceholder.typicode.com/posts/1")
.then(response => response.json())
.then(data => console.log(data));
Using async/await:
async function getPost() {
const res = await fetch("https://jsonplaceholder.typicode.com/posts/1");
const data = await res.json();
console.log(data);
}
getPost();
14. ES6+ Features You Must Know
-
letandconst - Arrow functions
- Template literals
- Destructuring
- Default parameters
- Spread operator
- Modules
Example:
const user = { name: "Farhad", age: 22 };
const { name, age } = user;
console.log(name, age);
15. Final Tips for Mastering JavaScript
- Build small projects regularly.
- Practice DOM manipulation.
- Learn ES6+ deeply.
- Explore Node.js for backend development.
- Read other developers’ code.
- Learn TypeScript when you reach intermediate.
Conclusion
JavaScript is a powerful and flexible language that anyone can learn with consistent practice. Whether you are building simple websites or full-scale applications, JavaScript provides the foundation you need to succeed.
Keep coding, keep experimenting, and keep growing.
Top comments (0)