@@ -47,6 +47,8 @@ Top JavaScript interview questions
47
47
| 39 | [ How do we add comments in javascript] ( #39-how-do-we-add-comments-in-javascript ) |
48
48
| 40 | [ What is the use of typeof operator] ( #40-what-is-the-use-of-typeof-operator ) |
49
49
| 41 | [ Is JavaScript case-sensitive] ( #41-is-javascript-case-sensitive ) |
50
+ | 42 | [ Difference between push() and unshift()] ( #42-difference-between-push-and-unshift ) |
51
+ | 43 | [ Difference between pop() and shift()] ( #43-difference-between-pop-and-shift ) |
50
52
51
53
### 1. What is JavaScript
52
54
* JavaScript is a scripting language used to create dynamic and interactive websites. It is supported by all major web browsers.
@@ -625,6 +627,43 @@ Yes, JavaScript is a case-sensitive language. For e.g., the variables firstName
625
627
let firstName = " Surbhi" ;
626
628
console .log (firstname); // output ========> Uncaught ReferenceError: firstname is not defined
627
629
` ` `
630
+
631
+ ### 42. Difference between push() and unshift()
632
+ Both are used to add elements to an array, but they add elements in different ways.
633
+
634
+ **push()** - It adds one or more elements to the end of an array and returns the new length of the array.
635
+ ` ` ` js
636
+ let arr = [1 ,2 ,3 ,4 ];
637
+ let newArr = arr .push (5 ,6 ,7 );
638
+ console .log (newArr); // output ========> 7
639
+ console .log (arr); // output ========> [1, 2, 3, 4, 5, 6, 7]
640
+ ` ` `
641
+ **unshift()** - It adds one or more elements to the beginning of an array and returns the new length of the array.
642
+ ` ` ` js
643
+ let arr = [1 ,2 ,3 ,4 ];
644
+ let newArr = arr .unshift (5 ,6 ,7 );
645
+ console .log (newArr); // output ========> 7
646
+ console .log (arr); // output ========> [5, 6, 7, 1, 2, 3, 4]
647
+ ` ` `
648
+
649
+ ### 43. Difference between pop() and shift()
650
+ Both are used to remove elements from an array, but they remove elements in different ways.
651
+
652
+ **pop()** - It removes the last element of an array and returns the removed element.
653
+ ` ` ` js
654
+ let arr = [1 ,2 ,3 ,4 ];
655
+ let newArr = arr .pop ();
656
+ console .log (newArr); // output ========> 4
657
+ console .log (arr); // output ========> [1,2,3]
658
+ ` ` `
659
+
660
+ **shift()** - It removes the first element of an array and returns the removed element.
661
+ ` ` ` js
662
+ let arr = [1 ,2 ,3 ,4 ];
663
+ let newArr = arr .shift ();
664
+ console .log (newArr); // output ========> 1
665
+ console .log (arr); // output ========> [2,3,4]
666
+ ` ` `
628
667
******************************In progress
629
668
630
669
0 commit comments