Master JavaScript programming with our comprehensive tutorial. Learn modern JavaScript features and asynchronous programming through examples.
JavaScript is a versatile programming language that enables interactive web pages and is essential for modern web development.
// Hello World in JavaScript
console.log("Hello World!");
console.log("Welcome to JavaScript!");Simple Hello World program in JavaScript
Learn fundamental JavaScript concepts including variables, data types, and basic operations.
// Variables and data types
let number = 42;
const pi = 3.14159;
let isValid = true;
let name = "John";
console.log("Number:", number);
console.log("Pi:", pi);
console.log("Is Valid:", isValid);
console.log("Name:", name);
// Modern ES6+ features
const template = `Hello ${name}`;
console.log("Template:", template);
const [x, y] = [1, 2];
console.log("Destructured x:", x, "y:", y);Variables, data types, and modern JavaScript features
// Arrays and Objects
const numbers = [1, 2, 3, 4, 5];
const person = {
name: "John",
age: 30,
greet() {
console.log(`Hello, I'm ${this.name}`);
}
};
console.log("Numbers:", numbers);
person.greet();
// Array methods
const doubled = numbers.map(n => n * 2);
const sum = numbers.reduce((a, b) => a + b, 0);
console.log("Doubled:", doubled);
console.log("Sum:", sum);Working with arrays and objects in JavaScript
JavaScript functions are first-class objects and can be used in various ways.
// Function declarations
function greet(name) {
return `Hello, ${name}!`;
}
// Arrow functions
const multiply = (a, b) => a * b;
// Higher-order functions
const twice = (f) => (x) => f(f(x));
const addOne = x => x + 1;
console.log(twice(addOne)(5)); // 7Different ways to work with functions in JavaScript
Learn how to handle asynchronous operations using Promises and async/await.
// Promises example
const myPromise = new Promise((resolve, reject) => {
setTimeout(() => {
resolve("Promise resolved!");
}, 1000);
});
myPromise
.then(result => console.log(result))
.catch(error => console.error(error));
// Async/Await example
async function processData() {
console.log("Processing...");
// Simulate async operation
await new Promise(resolve => setTimeout(resolve, 500));
console.log("Data processed successfully!");
return "Complete";
}
processData().then(result => console.log(result));Working with Promises and async/await in JavaScript