Skip to content

Latest commit

 

History

History
95 lines (67 loc) · 5.4 KB

13-ES14-2023.md

File metadata and controls

95 lines (67 loc) · 5.4 KB

ES14-2023 Questions

Array.findLast Method

1. How does the findLast method work in arrays, and what is its use?

The findLast method is a new addition to arrays that allows you to start from the end of an array and find the first element that matches a certain condition. This method works similarly to find, but instead of starting from the beginning of the array, it starts from the end. This is especially useful when you want to start searching from the end and find the first match.

const temp = [27, 28, 30, 40, 42, 35, 30];
let high = temp.findLast(x => x > 40);
console.log(high); // 42

In this example, findLast starts from the end of the array and finds the first value greater than 40, which is 42. This method is great when you need the last match for a specific condition.

Array.findLastIndex Method

2. What does the findLastIndex method do, and how can it optimize searches in arrays?

The findLastIndex method is similar to findLast, but instead of returning the value, it returns the index (position) of the last element that meets a certain condition. This method is useful when you want to know where a specific condition was last met in the array.

const temp = [27, 28, 30, 40, 42, 35, 30];
let pos = temp.findLastIndex(x => x > 40);
console.log(pos); // 4

In this example, findLastIndex finds the last position where a value greater than 40 exists, which is index 4 (value 42). This method is useful for optimizing searches and finding the location of the last match.

Array.toReversed Method

3. How is toReversed different from reverse, and why is it better to use?

The toReversed method is a new and safer way to reverse arrays that returns a new array without modifying the original one. Unlike reverse, which directly changed the original array, toReversed keeps the original array unchanged and returns a reversed copy.

const months = ["Jan", "Feb", "Mar", "Apr"];
const reversed = months.toReversed();
console.log(reversed); // ["Apr", "Mar", "Feb", "Jan"]
console.log(months); // ["Jan", "Feb", "Mar", "Apr"]

In this example, toReversed returns a reversed version of the months array without altering the original array. This method is useful when you need to reverse an array but don't want the original array to change.

Array.toSorted Method

4. How does toSorted help with sorting arrays, and why is it better than using sort?

The toSorted method is a new way to sort arrays that returns a sorted copy of the array without modifying the original one. This method works like sort, but its advantage is that it keeps the original array intact.

const months = ["Jan", "Feb", "Mar", "Apr"];
const sorted = months.toSorted();
console.log(sorted); // ["Apr", "Feb", "Jan", "Mar"]
console.log(months); // ["Jan", "Feb", "Mar", "Apr"]

In this example, toSorted returns a sorted array without changing the original one. This method is ideal when you need to sort but don't want the original array to be altered.

Array.toSpliced Method

5. How can toSpliced be useful in manipulating arrays, and how does it differ from splice?

The toSpliced method is a new and safe way to remove or replace elements in an array that returns a modified copy without altering the original array. This method is a better alternative to splice because splice would change the original array, but toSpliced does not.

const months = ["Jan", "Feb", "Mar", "Apr"];
const spliced = months.toSpliced(0, 1);
console.log(spliced); // ["Feb", "Mar", "Apr"]
console.log(months); // ["Jan", "Feb", "Mar", "Apr"]

In this example, toSpliced returns a new version of the months array with the first element removed. The original array remains unchanged. This method is great for when you want to make changes to an array but keep the original intact.

Array.with Method

6. How does the with method work, and what is its use in updating array elements?

The with method is a new way to replace elements in an array that returns a new version of the array with the specified element replaced, without modifying the original array. This method is useful for updating specific elements in an array without changing the rest of the array.

const months = ["Januar", "Februar", "Mar", "April"];
const updated = months.with(2, "March");
console.log(updated); // ["Januar", "Februar", "March", "April"]
console.log(months); // ["Januar", "Februar", "Mar", "April"]

In this example, with returns a new version of the months array where the third element ("Mar") is replaced with "March". This allows you to easily update the array without changing the original one.

#! (Shebang) Feature

7. What role does the #! (Shebang) feature play in executing JavaScript scripts, and why is it important to use?

The #! feature, also known as Shebang, is added to script files to tell the operating system which interpreter should be used to run the file. This feature is particularly useful when you want to run JavaScript code directly in Unix or Linux environments.

#!/usr/bin/env node
console.log('Hello World');

In this example, the #! indicates that the script should be executed with node. This allows you to run JavaScript code directly with ./fileName.js instead of using node fileName.js. This feature helps manage and execute scripts more easily in different environments.