Skip to main content

Featured

๐Ÿ”  Day 8 — Strings & String Methods in JavaScript

๐Ÿ”  Day 8 — Strings & String Methods in JavaScript Welcome to Day 8 ๐ŸŒŸ — Today’s session is all about **Strings**, one of the most common data types in JavaScript. Strings represent text and allow you to store names, messages, or any sequence of characters. ๐Ÿ’ฌ 1️⃣ What is a String? A string is a sequence of characters written inside quotes. let name = "Rahul"; let greeting = 'Hello World'; let message = `Welcome ${name}!`; // Template literal ๐Ÿงฉ 2️⃣ String Properties Strings behave like arrays — they have a .length property and can be accessed using index numbers. let text = "JavaScript"; console.log(text.length); // 10 console.log(text[0]); // J console.log(text[text.length - 1]); // t ๐ŸŽจ 3️⃣ Common String Methods toUpperCase() & toLowerCase() let city = "Hyderabad"; console.log(city.toUpperCase()); console.log(city.toLowerCase()); slice(start, end) let word = "JavaScript"; console.log(word.slice(0, 4)...

๐Ÿงฉ Day 7 — Arrays & Array Methods in JavaScript

๐Ÿงฉ Day 7 — Arrays & Array Methods in JavaScript

Welcome to Day 7 ๐ŸŽ‰ — Today we’ll dive into one of the most essential data structures in JavaScript — the Array. Arrays allow you to store multiple values in a single variable, making it easy to handle collections of data like numbers, names, or products.

๐Ÿ“ฆ 1️⃣ What is an Array?

An array is a special variable that can hold multiple values at once. Instead of creating several variables, you can group values inside square brackets [].

let fruits = ["Apple", "Banana", "Cherry"];
console.log(fruits); // ["Apple", "Banana", "Cherry"]

๐Ÿ” 2️⃣ Accessing Elements

Each element in an array has an index (position), starting from 0.

console.log(fruits[0]); // Apple
console.log(fruits[2]); // Cherry

➕ 3️⃣ Modifying Arrays

You can change values using their index or add new ones dynamically.

fruits[1] = "Mango"; // Change Banana to Mango
fruits.push("Grapes"); // Add to end
fruits.unshift("Orange"); // Add to start
console.log(fruits);

๐Ÿงน 4️⃣ Removing Elements

There are multiple ways to remove elements from arrays:

fruits.pop(); // Removes last
fruits.shift(); // Removes first
fruits.splice(1, 1); // Removes one element at index 1

⚙️ 5️⃣ Common Array Methods

Let’s explore the most used methods to process arrays:

  • .length — Returns total elements
  • .indexOf() — Finds index of a value
  • .includes() — Checks if value exists
  • .concat() — Merges two arrays
let colors = ["Red", "Blue", "Green"];
console.log(colors.length);     // 3
console.log(colors.indexOf("Blue")); // 1
console.log(colors.includes("Yellow")); // false

๐Ÿงฉ 6️⃣ Array Iteration (map, filter, forEach)

forEach()

Executes a function for each array element.

colors.forEach((color) => console.log(color));

map()

Creates a new array with transformed values.

let upperColors = colors.map((c) => c.toUpperCase());
console.log(upperColors);

filter()

Returns a new array with only elements that match a condition.

let numbers = [10, 20, 30, 40];
let above20 = numbers.filter((num) => num > 20);
console.log(above20); // [30, 40]

๐Ÿง  7️⃣ Practice Task

  1. Create an array of your top 5 favorite movies.
  2. Add and remove a movie using push() and pop().
  3. Use map() to make them all uppercase.
  4. Filter movies with more than 5 letters.

๐ŸŽฏ Summary

Arrays are essential for grouping and processing data efficiently. Tomorrow, we’ll explore **Strings and String Methods**, another core part of JavaScript data handling.

Comments