Welcome to the fundamental guide to JavaScript! This tutorial covers the core concepts you need to start building dynamic and interactive web experiences.
JavaScript (JS) is a versatile, high-level programming language that enables you to create dynamic and interactive content on web pages. It's one of the three core technologies of the World Wide Web, alongside HTML and CSS. While HTML structures content and CSS styles it, JavaScript brings websites to life.
Key features of JavaScript include:
JavaScript code consists of statements that perform actions. Variables are used to store data values.
You can declare variables using var
, let
, or const
.
var
: Function-scoped or globally scoped.let
: Block-scoped, can be reassigned.const
: Block-scoped, cannot be reassigned (constant).
let message = "Hello, JavaScript!";
const year = 2023;
var count = 10;
console.log(message);
console.log("Current year: " + year);
JavaScript has several primitive data types:
"Hello"
).10
, 3.14
).true
or false
.Complex data types include Objects (including Arrays and Functions).
Operators are symbols that perform operations on variables and values.
let x = 10;
let y = 5;
console.log(x + y); // Addition: 15
console.log(x - y); // Subtraction: 5
console.log(x * y); // Multiplication: 50
console.log(x / y); // Division: 2
console.log(x % y); // Modulus (Remainder): 0
console.log(x > y); // Greater than: true
console.log(x === '10'); // Strict equality: false (checks type and value)
console.log(x == '10'); // Loose equality: true (coerces types)
let isAdult = true;
let hasPermission = false;
console.log(isAdult && hasPermission); // AND: false
console.log(isAdult || hasPermission); // OR: true
console.log(!isAdult); // NOT: false
Control flow statements allow you to execute code blocks conditionally or repeatedly.
Example:
let temperature = 25;
if (temperature > 30) {
console.log("It's hot!");
} else if (temperature > 20) {
console.log("It's pleasant.");
} else {
console.log("It's cool.");
}
Example: `for` loop
for (let i = 0; i < 5; i++) {
console.log("Iteration number: " + (i + 1));
}
Example: `for...of` loop (for arrays)
const fruits = ["Apple", "Banana", "Cherry"];
for (const fruit of fruits) {
console.log(fruit);
}
Functions are blocks of reusable code that perform a specific task.
Example:
function greet(name) {
return "Hello, " + name + "!";
}
let greetingMessage = greet("Alice");
console.log(greetingMessage); // Output: Hello, Alice!
// Arrow function (ES6+)
const add = (a, b) => a + b;
console.log(add(5, 3)); // Output: 8
These are fundamental data structures.
An array is a special variable, which can hold more than one value at a time.
const colors = ["Red", "Green", "Blue"];
console.log(colors[0]); // Accessing the first element: "Red"
colors.push("Yellow"); // Adding an element
console.log(colors.length); // Number of elements: 4
Objects are collections of key-value pairs.
const person = {
firstName: "John",
lastName: "Doe",
age: 30,
isStudent: false
};
console.log(person.firstName); // Accessing a property: "John"
console.log(person['age']); // Another way to access: 30
JavaScript allows you to dynamically change the content, structure, and style of a web page. This is known as Document Object Model (DOM) manipulation.
Note: DOM manipulation requires an HTML document to operate on. The following examples illustrate concepts:
Example: Changing text content
// Assuming you have an element with id="myHeading"
// const heading = document.getElementById('myHeading');
// heading.textContent = 'New Heading Text';
Example: Adding an event listener
// Assuming you have a button with id="myButton"
// const button = document.getElementById('myButton');
// button.addEventListener('click', function() {
// alert('Button clicked!');
// });
Asynchronous JavaScript allows you to perform operations (like fetching data from a server) without freezing the browser. This is crucial for responsive web applications.
Key concepts include callbacks, Promises, and async/await
.
Example: Using setTimeout
(a basic asynchronous function)
console.log("Start");
setTimeout(function() {
console.log("This will run after 2 seconds");
}, 2000);
console.log("End");
// Output: Start, End, This will run after 2 seconds