avatarKirti K

Summary

The provided web content is a comprehensive guide on JavaScript array manipulation, offering methods to check for element existence and remove elements from arrays, aimed at beginners preparing for JavaScript interviews.

Abstract

The web content titled "JavaScript Day 14 — Mastering JavaScript Basics: A Comprehensive Guide for Interview Preparation" is part of a "100 Days of JavaScript Interview Prep: A Beginner’s Journey" series. It focuses on array manipulation techniques in JavaScript, including how to check if an element exists within an array and how to remove an element from an array using various methods. The guide explains the indexOf(), includes(), and some() methods for checking element existence, and demonstrates the splice(), filter(), and pop() methods for removing elements. The article emphasizes practical learning through examples and encourages sharing knowledge, kindness, and respect within the programming community.

Opinions

  • The author, Kirti Kaushal, values the importance of a structured learning journey, as evidenced by the 100-day plan for mastering JavaScript basics.
  • The author believes in the practical application of knowledge, providing real code examples to illustrate array manipulation methods.
  • The article conveys that learning should be a communal effort, with encouragement to share knowledge and support each other.
  • The author suggests that kindness is a fundamental trait to possess, emphasizing its importance alongside gaining technical skills.
  • Kirti Kaushal appreciates reader support, inviting followers to subscribe and engage with the content to foster a collaborative learning environment.

JavaScript Day 14 — Mastering JavaScript Basics: A Comprehensive Guide for Interview Preparation

100 Days of JavaScript Interview Prep: A Beginner’s Journey

Welcome to Day 14–100 Days of JavaScript Interview Prep: A Beginner’s Journey — Kirti Kaushal

Welcome to Day 14–100 Days of JavaScript Interview Prep: A Beginner’s Journey

Embarking on a 100-day adventure focused on learning, practice, and growth sounds like an incredibly rewarding journey.

DAY 14 (April 5th, 2024) — Arrays in JavaScript with examples?

Question 1 — How do you check if an element exists in an array with examples ?

Answer :

We can check if an element exists in an array using various methods in JavaScript.

Here are few common ways with example

  1. Using indexOf() Method:

The indexOf() method of Array instances returns the first index at which a given element can be found in the array, or -1 if it is not present.

const fruits= ['apple', 'mango', 'banana'];
console.log(fruits.indexOf('mango'));
// output === 1
// Start from index 2
console.log(fruits.indexOf('apple', 2));
// output ===-1
console.log(fruits.indexOf('banana'));
// output === 2
How do you check if an element exists in an array with examples ?

2. ES6+ Method — Using includes() :

The includes() method of Array instances determines whether an array includes a certain value among its entries, returning true or false as appropriate.

const array1 = [4, 5, 6];
console.log(array1.includes(6));
// Expected output: true
console.log(array1.includes(9));
// Expected output: false

How do you check if an element exists in an array with examples ?

3. Using Some() Method

The some() method of Array instances tests whether at least one element in the array passes the test implemented by the provided function.

It returns true if, in the array, it finds an element for which the provided function returns true; otherwise it returns false.

It doesn't modify the array.

const array = [1, 2, 3, 4, 5];
// Checks whether an element is even
const even = (element) => element % 2 === 0;
console.log(array.some(even));
// Expected output: true
How do you check if an element exists in an array with examples ?

Question 2 — How to remove an element of array with different approaches?

Answer

  1. Using the splice() Method:

The splice() method changes the contents of an array by removing or replacing existing elements and/or adding new elements in place.

let alphabets = ['A', 'B', 'C', 'D'];
// Remove 'B' from the array
let index = alphabets.indexOf('B');
if (index !== -1) {
    alphabets.splice(index, 1);
}
console.log(alphabets); // Output: ['A', 'C', 'D']
How to remove an element of array with different approaches?

2. Using the filter() Method

The filter() method creates a new array with all elements that pass the test implemented by the provided function.

let alphabets = ['A', 'B', 'C', 'D'];
// Remove 'B' from the array
alphabets = alphabets.filter(fruit => fruit !== 'B');
console.log(alphabets)
How to remove an element of array with different approaches?

3. Using the pop() Method:

The method removes the last element from an array and returns that element.

It depends on what type of removal required

let alphabets = ['A', 'B', 'C', 'D'];
alphabets.pop()
console.log(alphabets) // A, B, C

Happy Coding and Stay Tuned :)

Kind is cool !!!

Share knowledge to gain knowledge !!!!

Be kind and treat people with respect !!

If you find my videos helpful, then please subscribe, Claps and follow to support :)

Cheers Kirti Kaushal

Other Articles :

Coding Interviews
Javascript Tips
Javascript Interview
Beginners Guide
Programming
Recommended from ReadMedium