JavaScript Tutorial

JavaScript is a powerful, versatile language widely used for web development, allowing developers to add interactivity, animation, and other dynamic features to websites. Originally designed as a client-side scripting language for browsers, JavaScript is now also used on the server side with platforms like Node.js, making it a full-stack language. This tutorial will guide you through the basics of JavaScript, covering syntax, data types, functions, control structures, and more.

Whether you’re new to programming or looking to add JavaScript to your skill set, this tutorial will provide you with the foundation to get started.


Getting Started with JavaScript

To start coding in JavaScript, you only need a web browser (like Chrome, Firefox, or Edge) and a text editor (such as VS Code or Notepad). JavaScript code can be embedded directly into HTML files or saved in external .js files.

Hello, World in JavaScript

Let’s begin with a simple program that displays “Hello, World!” on the browser console:

<!DOCTYPE html>
<html lang="en">
<head>
  <title>Hello, JavaScript!</title>
</head>
<body>
  <script>
    console.log("Hello, World!");
  </script>
</body>
</html>

To run this code, save it as an HTML file and open it in your browser. Right-click and select “Inspect” or press Ctrl+Shift+I to open the Developer Console and see the message “Hello, World!” printed there.


JavaScript Basics

Variables

In JavaScript, variables are used to store data. You can declare a variable using var, let, or const.

  • var is an older keyword that is function-scoped.
  • let is block-scoped and is generally preferred for mutable variables.
  • const is also block-scoped and used for constants.
let name = "Alice"; // Block-scoped variable
const age = 25; // Constant variable

Data Types

JavaScript supports various data types, including:

  • String: "Hello"
  • Number: 42
  • Boolean: true or false
  • Object: { name: "Alice", age: 25 }
  • Array: [1, 2, 3]
  • Undefined: A variable without an assigned value
  • Null: A variable with no value
let isStudent = true; // Boolean
let score = 92.5; // Number
let person = { name: "Bob", age: 30 }; // Object
let colors = ["red", "green", "blue"]; // Array

Operators in JavaScript

JavaScript offers a variety of operators:

  • Arithmetic operators: +, -, *, /, %
  • Comparison operators: ==, ===, !=, !==, <, >, <=, >=
  • Logical operators: && (AND), || (OR), ! (NOT)
let x = 10;
let y = 5;
console.log(x + y); // 15
console.log(x > y); // true
console.log(x === 10 && y === 5); // true

Control Structures

Conditionals

JavaScript uses if, else if, and else statements to control the flow of code based on conditions.

let age = 18;
if (age >= 18) {
  console.log("You are an adult.");
} else {
  console.log("You are a minor.");
}

Loops

Loops allow you to repeat code. Common types of loops include for, while, and do...while.

// For loop
for (let i = 0; i < 5; i++) {
  console.log("Iteration:", i);
}

// While loop
let count = 0;
while (count < 3) {
  console.log("Count is:", count);
  count++;
}

Functions

Functions are blocks of reusable code that can be called with parameters to perform a specific task.

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

console.log(greet("Alice")); // Hello, Alice!

Arrow Functions

Arrow functions provide a shorter syntax for writing functions and are popular in modern JavaScript.

const add = (a, b) => a + b;
console.log(add(3, 4)); // 7

Objects and Arrays

Objects

Objects in JavaScript are collections of properties and methods.

let car = {
  brand: "Toyota",
  model: "Corolla",
  year: 2020,
  start() {
    console.log("Car started");
  },
};

console.log(car.brand); // Toyota
car.start(); // Car started

Arrays

Arrays store multiple values in a single variable. Each value is accessed by its index.

let fruits = ["apple", "banana", "orange"];
console.log(fruits[1]); // banana

// Array methods
fruits.push("grape"); // Add an item
console.log(fruits.length); // Get array length

Advanced JavaScript Concepts

Asynchronous JavaScript

JavaScript uses asynchronous programming with callbacks, promises, and async/await to handle tasks that take time, such as fetching data.

Promises

A promise represents a value that may be available now, later, or never.

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

promise.then((message) => console.log(message)); // Data loaded

Async/Await

Async/await simplifies working with promises, making code easier to read.

async function fetchData() {
  let data = await promise;
  console.log(data);
}
fetchData(); // Data loaded

JavaScript in the Browser

JavaScript can manipulate HTML and CSS to create interactive webpages. Here are some common tasks:

DOM Manipulation

The Document Object Model (DOM) represents the HTML structure. JavaScript can modify HTML elements by selecting them and changing their properties.

<!DOCTYPE html>
<html lang="en">
<body>
  <p id="text">Original text</p>
  <button onclick="changeText()">Change Text</button>

  <script>
    function changeText() {
      document.getElementById("text").innerHTML = "Text changed!";
    }
  </script>
</body>
</html>

Events

JavaScript can respond to events, such as clicks or keypresses, making webpages interactive.

document.getElementById("text").addEventListener("click", () => {
  alert("Text was clicked!");
});

JavaScript Frameworks and Libraries

Several popular libraries and frameworks simplify JavaScript development, especially for building complex applications:

  • jQuery: A lightweight library that simplifies DOM manipulation and event handling.
  • React: A library developed by Facebook for building user interfaces, especially single-page applications.
  • Vue.js: A progressive framework for building UIs with a component-based architecture.
  • Angular: A full-featured framework maintained by Google for building large applications.

These tools extend JavaScript’s capabilities, making it easier to create responsive, dynamic user interfaces.


Conclusion

JavaScript is a versatile and powerful language that’s essential for modern web development. From basic syntax to advanced concepts, this tutorial provides the foundation you need to start coding in JavaScript. As you gain experience, you’ll find opportunities to create interactive websites, server-side applications, and even mobile apps using JavaScript and its ecosystem.


Table of contents