
Explaining: Strings, Variables, Numbers, Operators, Functions, Arrays, Indexes, Mutability, .length, Objects, Key-Value Pairs, Dot Notation, Loops, .map(), .filter()
Here’s a detailed explanation of each concept in JavaScript with examples:
- Strings: Strings are sequences of characters enclosed in single (”) or double (“”) quotes. They are used to represent textual data.
Example:
let message = "Hello, World!";
console.log(message); // Output: Hello, World!
- Variables: Variables are used to store and manipulate data. In JavaScript, you can declare variables using the
let
keyword.
Example:
let age = 25;
console.log(age); // Output: 25
- Numbers: Numbers are used to perform mathematical operations. They can be integers or floating-point numbers.
Example:
let x = 5;
let y = 10;
let sum = x + y;
console.log(sum); // Output: 15
- Operators: Operators are used to perform operations on variables and values. JavaScript includes arithmetic, assignment, comparison, logical, and more.
Example:
let a = 5;
let b = 3;
let result = a * b;
console.log(result); // Output: 15
- Functions: Functions are reusable blocks of code that perform a specific task. They can take input values called parameters and return a value.
Example:
function greet(name) {
console.log("Hello, " + name + "!");
}
greet("John"); // Output: Hello, John!
- Arrays: Arrays are used to store multiple values in a single variable. They can contain any combination of data types.
Example:
let numbers = [1, 2, 3, 4, 5];
console.log(numbers[2]); // Output: 3
- Indexes: Indexes are used to access individual elements in an array. They start from 0 for the first element.
Example:
let message = "Hello";
console.log(message[0]); // Output: H
- Mutability: Mutability refers to the ability to change or modify values. In JavaScript, strings and numbers are immutable, while arrays and objects are mutable.
Example:
let person = {
name: "John",
age: 30,
};
person.age = 31;
console.log(person.age); // Output: 31
- .length: The
.length
property is used to retrieve the length of a string or the number of elements in an array.
Example:
let text = "Hello, World!";
console.log(text.length); // Output: 13
- Objects: Objects are collections of key-value pairs. They are used to represent more complex data structures and can hold different data types.
Example:
let car = {
brand: "Toyota",
color: "blue",
};
console.log(car.brand); // Output: Toyota
- Key-Value Pairs: Key-value pairs are used to define properties within an object. The key represents the property name, and the value represents the associated value.
Example:
let person = {
name: "John",
age: 30,
};
console.log(person.name); // Output: John
- Dot Notation: Dot notation is used to access properties or call methods of an object using dot
.
followed by the property or method name.
Example:
let student = {
name: "Alice",
grade: "A",
};
console.log(student.name); // Output: Alice
- Loops: Loops are used to repeat a block of code multiple times. Common loop types in JavaScript are
for
,while
, anddo-while
.
Example:
for (let i = 0; i < 5; i++) {
console.log(i);
}
// Output:
// 0
// 1
// 2
// 3
// 4
- .map(): The
.map()
method is used to create a new array by applying a callback function to each element of an existing array.
Example:
let numbers = [1, 2, 3, 4, 5];
let squaredNumbers = numbers.map((num) => num * num);
console.log(squaredNumbers); // Output: [1, 4, 9, 16, 25]
- .filter(): The
.filter()
method is used to create a new array containing elements from an existing array that satisfy a given condition.
Example:
let numbers = [1, 2, 3, 4, 5];
let evenNumbers = numbers.filter((num) => num % 2 === 0);
console.log(evenNumbers); // Output: [2, 4]
These examples should provide you with a detailed understanding of each concept in JavaScript and how they can be applied. Feel free to experiment with them and explore further!