1. Explain about Arrays in JavaScript?
An Array holds an ordered sequence of items.
- They are used to store multiple values in a single variable.
- The typeofoperator in JavaScript returnsobjectfor arrays.
2. How to compare two arrays?
In JavaScript, to compare two arrays,
The length of both the arrays should be the same. The type and value of each item in one array should be compared with the counterpart in another array.
JavaScript provides a function
JSON.stringify()in order to convert an object or array into JSON string. By converting into JSON string we can directly check if the strings are equal or not.
3. How can you create an Array in JavaScript?
Creating an Array using the Array Literal
Creating an Array using the JavaScript Keyword new
4. Explain about reduce, map, filter methods?
reduce():
The
The result is a single value. It is the result of running the reducer across all elements of the array.
Example:
In each step, the reducer function adds the current array element to the result from the previous step until there are no more elements to add.
map():
The
Example:
filter():
- The filtermethod creates a new array filled with all elements that pass the test (provided as a function).
- A new array with the elements that pass the test will be returned. If no elements pass the test, an empty array will be returned.
Example:
5. What is the use of the push method in JavaScript?
The
6. Write a sample program for map and filter?
Sample program for map:
Sample program for filter:
7. What is the use of a map method?
- Instead of manually iterating over the array using a loop, we can simply use the built-in mapmethod.
- The mapmethod allows you to iterate over an array and modify its elements using a callback function. The callback function will then be executed on each of the array's elements.
8. Explain about the array method slice?
The
It returns a portion between the specified start index and end index(end index not included) of an array into a new array.
Syntax:
Example:
9. How do you empty an array??
There are several ways to empty an array. The simplest one is to reassign the variable to an empty array.
Examples:
10. What are the most useful JavaScript array methods?
Some of the most useful JavaScript array methods are:
push()
- The pushmethod adds elements to an array at the last position.
- The
pop()
- The popmethod removes the last element of an array and returns it.
- The
forEach()
- The forEachmethod executes a provided function once for each array element. It always returns undefined.
- The
map()
- The mapmethod creates a new array with the results of calling the provided function for every array element.
- The
filter()
- The filtermethod creates a new array filled with all elements that pass the test (provided as a function).
- The
splice()
- The splicemethod changes the contents of an array by removing or replacing existing elements and/or adding new elements.
- The
slice()
- The slicemethod returns a portion between the specified start index and end index(end index not included) of an array into a new array.
- The
shift()
- The shiftmethod removes the first element of an array.
- The
unshift()
- The unshiftmethod adds one or more elements to the beginning of an array.
- The
sort()
- The sortmethod sorts the items of an array and returns the sorted array. The default sort order is ascending.
- The
11. What are the differences between slice and splice?
Slice | Splice |
---|---|
Doesn't modify the original array(immutable) | Modifies the original array(mutable) |
Returns the subset of original array | Returns the deleted elements as array |
Used to pick the elements from array | Used to insert or delete elements to/from array |
12. Explain about pop and shift methods?
pop():
The
Example:
shift():
The
13. What is the difference between map and forEach methods?
Both of them iterate through the elements of an array. The difference is that the
14. Which JavaScript methods are used to search for an element in an array?
indexOf():
The
includes():
The
filter():
We can use the
find():
We use the
15. What is the purpose of the splice method?
The
Remove existing items
Syntax:
- Start: Starting Index
- Delete Count: Number of items to be removed, starting from the given index
The
Add new items
Syntax:
Here the
Replace existing items
Syntax:
16. How to sort the numbers in an array?
By default, the sort method sorts elements alphabetically. To sort numerically just add the Compare Function that handles numeric sorts:
Compare Function:
When the
If the result is negative, a is sorted before b.
If the result is positive, b is sorted before a.
If the result is 0, no changes are done with the sort order of the two values.
Example:
The compare function compares all the values in the array, two values at a time (a, b).
When comparing 40 and 100, the
sortmethod calls the compare function(40, 100).The function calculates 40 - 100 (a - b), and since the result is negative (-60), the
sortmethod will sort 40 as a value lower than 100.
Example:
17. What is an object and explain object creation?
An Object is a collection of properties, and a property is an association between a name (or key) and a value.
In JavaScript, there are four ways to create an object:
Object Literals:
An object literal also called an object initializer, is a comma-separated set of paired names and values.
New operator or constructor:
If we call a function using a new operator, the function acts as a constructor and returns an object.
Object.create method
We can also create new objects using the
We can use the Car object as a prototype to create another object, as shown below:
Class
We can use the
We can use the Car class to create objects as shown below:
18. What are object prototypes?
A prototype is a blueprint of an object. The prototype allows us to use properties and methods on an object even if the properties and methods do not exist on the current object.
All JavaScript objects inherit properties from a prototype.
Date objects inherit properties from the Date prototype.
Math objects inherit properties from the Math prototype.
Array objects inherit properties from the Array prototype.
Example:
19. How to add key-value pair to an object in JavaScript?
There are two possible solutions to add new properties to an object.
Using dot notation: This solution is useful when you know the name of the property.
Example:
Using square bracket notation: This solution is useful when the name of the property is dynamically determined.
Example:
20. What is Object Destructuring?
To unpack properties from Objects, we use Object Destructuring. The variable name should match with the key of an object.
Example:
21. In the arrow functions, what is the significance of 'this' keyword?
In Arrow functions,
- When the code is defined
- Context
Arrow function inherits
22. What is the use of 'this' keyword in JavaScript?
The