Skip to main content

Complete JavaScript Tutorial: Learn JavaScript from Basics to Advanced (ES6, DOM, Async)

 Complete Guide to JavaScript

1. Introduction to JavaScript

JavaScript is one of the core technologies of the web, alongside HTML and CSS. It was created by Brendan Eich in 1995 and has evolved into a powerful language used for both client-side and server-side development.



Key Features of JavaScript:

  • Interpreted: Runs directly in the browser without needing compilation.

  • Dynamic Typing: Variables can hold any type of data.

  • Event-Driven: Listens to user interactions and executes code accordingly.

  • Supports Object-Oriented, Functional, and Procedural Programming.

  • Asynchronous Processing: Handles concurrent tasks efficiently.

2. JavaScript Basics

Variables and Data Types

JavaScript supports three types of variable declarations:

javascript
var name = "John"; // Function-scoped let age = 25; // Block-scoped const PI = 3.1416; // Immutable constant

Common data types:

  • String"Hello"

  • Number42

  • Booleantrue, false

  • Array[1, 2, 3]

  • Object{name: "Alice", age: 30}

  • Null & Undefined

Operators

  • Arithmetic+-*/%

  • Comparison=====!=!==><

  • Logical&&||!

3. Control Flow and Loops

Conditional Statements

javascript
if (age > 18) { console.log("Adult"); } else { console.log("Minor"); }

Loops

javascript
for (let i = 0; i < 5; i++) { console.log(i); } let j = 0; while (j < 5) { console.log(j); j++; }

4. Functions and Scope

Function Declaration

javascript
function greet(name) { return `Hello, ${name}`; }

Arrow Function

javascript
const greet = (name) => `Hello, ${name}`;

Scope

  • Global Scope: Variables accessible anywhere.

  • Local Scope: Variables defined within a function.

  • Block Scope: Variables defined inside {} blocks.

5. Objects and Arrays

Objects

javascript
let person = { name: "John", age: 30, greet() { console.log("Hello!"); } };

Arrays and Methods

javascript
let numbers = [1, 2, 3, 4]; console.log(numbers.map(num => num * 2));

6. DOM Manipulation

Selecting Elements

javascript
let btn = document.getElementById("myButton"); let div = document.querySelector(".myClass");

Modifying Elements

javascript
div.innerHTML = "Updated Content";

Event Listeners

javascript
btn.addEventListener("click", () => { alert("Button clicked!"); });

7. Asynchronous JavaScript

Callbacks

javascript
function fetchData(callback) { setTimeout(() => { callback("Data received"); }, 2000); }

Promises

javascript
let promise = new Promise((resolve, reject) => { setTimeout(() => resolve("Success"), 2000); });

Async/Await

javascript
async function fetchData() { let response = await fetch("https://api.example.com"); let data = await response.json(); console.log(data); }

8. ES6+ Features

Destructuring

javascript
let person = { name: "Alice", age: 30 }; let { name, age } = person;

Spread Operator

javascript
let arr1 = [1, 2, 3]; let arr2 = [...arr1, 4, 5];

9. Working with APIs

Fetch API

javascript
fetch("https://api.example.com") .then(response => response.json()) .then(data => console.log(data));

10. JavaScript in the Backend (Node.js)

Setting up a Server with Express.js

javascript
const express = require("express"); const app = express(); app.get("/", (req, res) => { res.send("Hello World"); }); app.listen(3000);

11. Advanced Concepts

Prototypes and Inheritance

javascript
function Person(name) { this.name = name; } Person.prototype.greet = function() { console.log("Hello " + this.name); };

Event Loop and Concurrency

JavaScript uses an event loop to handle asynchronous tasks without blocking execution.

12. Best Practices

✅ Use let and const instead of var.
✅ Keep code modular.
✅ Handle errors properly with try-catch.
✅ Avoid global variables.

13. JavaScript in Modern Development

JavaScript is used in:

  • Frontend Frameworks: React, Vue, Angular

  • Backend: Node.js

  • Mobile Apps: React Native

  • Game Development: Phaser.js

  • Machine Learning: TensorFlow.js

Conclusion

JavaScript is a versatile language shaping modern web and application development. Mastering it opens doors to various career opportunities in software engineering, web development, and beyond.