JavaScript is the backbone of web development, allowing you to build interactive and dynamic websites. If you're just starting out, learning its core concepts is crucial. Let's explore the fundamentals!
Variables are used to store information that your program can use later. JavaScript provides different types of variables (let, const, var).
let name = "Alice"; // Can be reassigned
const age = 25; // Cannot be reassigned
var isDeveloper = true; // is function-scoped, avoid using it (old syntax)
let firstName = "Alice"; // String
let age = 26; // Number
let isOnline = true; // Boolean
let unknownVariable; // Undefined
let emptyValue = null; // Null
let colors = ['red', 'green', 'blue']; // Array
let person = { name: "John", age: 30 }; // Object
Operators allow us to perform operations on values and variables. There are arithmetic operators (+, -, *, / ), comparison operators (==, ===, >, < ), and logical operators (&&, ||, !).
let sum = 10 + 5; // 15 (addition)
let difference = 10 - 5; // 5 (subtraction)
let product = 10 * 5; // 50 (multiplication)
let quotient = 10 / 5; // 2 (division)
let remainder = 10 % 3; // 1 (remainder of the division)
let isEqual = 10 == "10"; // (true) compare values only
let isStrictEqual = 10 === "10"; // (false) compare both value and type
let isGreater = 10 > 5; // (true) check if 10 is greater than 5
let isLess = 9 < 4; // (false) check if 9 is less than 4
let x = true;
let y = false;
console.log(x && y); // false (AND operator - both must be true)
console.log(x || y); // true (OR operator - at least one must be true)
console.log(!x); // false (NOT operator - inverts the boolean value)
Control flow structures like if-else and switch statements help in making decisions in code.
let age = 20;
if (age >= 18) {
console.log("You can vote!");
// Executes this block if age is 18 or more
} else {
console.log("You’re too young to vote.");
// Executes this block if age is less than 18
}
Explanation: The switch statement checks the value of day.
let day = 3;
switch (day) {
case 1:
console.log("Monday");
break;
case 2:
console.log("Tuesday");
break;
case 3:
console.log("Wednesday");
break;
case 4:
console.log("Thursday");
break;
case 5:
console.log("Friday");
break;
default:
console.log("Weekend or Invalid Day");
}
Functions allow you to group code into reusable blocks, making your program more efficient and modular.
function greet(name) {
return "Hello, " + name + "!";
// function that returns a greeting string
}
console.log(greet("Alice")); // Hello Alice!
Arrays are used to store multiple values in a single variable, while objects store data as key-value pairs.
let fruits = ["Apple", "Banana", "Cherry"];
console.log(fruits[0]); // Apple
let person = { name: "Alice", age: 25 };
console.log(person.name); // Alice
Loops allow you to execute a block of code multiple times, making it useful for handling repetitive tasks.
for (let i = 1; i <= 5; i++) {
console.log("Number:", i);
// Print number from 1 to 5
}
JavaScript ES6 introduced modern features like template literals and destructuring to improve code readability and efficiency.
Allow you to embed expressions inside string literals using backticks. They make string concatenation cleaner and more readable.
const name = "Alice";
console.log(`Hello, ${name}!`);
// ${name} is a placeholder for the variable value
Allows you to extract values from objects or arrays and assign them to variables in a concise way.
const person = { name: "Alice", age: 25 };
const { name } = person;
console.log(name); // Alice
let fruits = ["Apple", "Banana", "Cherry"];
const [firstFruit] = fruits;
console.log(firstFruit); // Apple
Provide a shorter and more concise syntax for writing functions in JavaScript. They are commonly used in modern JavaScript because of their readability and implicit return behavior.
const greet = (name) => `Hello, ${name}!`;
console.log(greet("Alice")); // Output: Hello, Alice!
const add = (a, b) => a + b;
console.log(add(5, 10)); // Output: 15
JavaScript uses try...catch to handle errors and prevent crashes in code execution.
try {
let result = riskyOperation();
// Trying to run a function that may cause an error
} catch (error) {
console.log("Something went wrong!", error);
// If there's an error, show the message
}
Mastering these JavaScript fundamentals will help you build interactive and dynamic applications. Keep practicing, and soon you'll be coding like a pro! 🚀