How to add value in array specific position | JavaScript Tutorials in Hindi | Interview Question #5
In JavaScript, you can add a value to a specific position in an array using the splice() method. The splice() method allows you to add, remove, or replace elements in an array at a specific position. This is frontend developer interview question. In JavaScript Tutorials in Hindi series we will cover all the important Javascript interview questions.
In the above video, we define an array arr with 4 elements. We then define the index 2 as the position where we want to add the new value. We define the value ‘march’ as the new value to add. We then use the splice() method to add the new value to the specified position. The first argument to splice() is the index at which to start changing the array. The second argument is the number of elements to remove (in this case, we want to add an element without removing anything, so we set it to 0). The third argument is the new value to add. Finally, we use console.log to print the updated arr.