1) What will be the output of the following code snippet?

      const obj1 = {Name: "Hello", Age: 16};
      const obj2 = {Name: "Hello", Age: 16};
      print(obj1 === obj2);

      The strict equality operator compares objects by their references so even though the contents of both objects are the same, their references don’t match resulting in false.

      2) What will be the output of the following code snippet?

      a = 5 + "9";
      document.write(a);

      Answer : 59 , the first number is of integer type and the second of string type. The code snippet gives priority to the string type and concatenates them due to the ‘+’ operator.

      3) What will be the output of the following code snippet?

      var x=12;
      var y=8;
      var res=eval("x+y");
      document.write(res);

      Answer : 20 , The eval() function evaluates the expression passed into it as a parameter and returns the result after evaluation.

      4) What will be the output of the following code snippet?

      var a = Math.max() < Math.min();
      var b = Math.max() > Math.min();
      console.log(a);
      console.log(b);

      In Javascript Math.max() is lesser than Math.min() because Math.max() returns -Infinity and Math.min() returns Infinity.

      5) What will be the output of the following code snippet?

      console.log(parseInt("123Hello"));
      console.log(parseInt("Hello123"));

      The parseInt() method converts a string to an integer if it is possible. If the string starts with a number, it will convert that numeric part and return it. Else, it will return NaN.

      6) What will be the output of the following code snippet?

      let sum = 0; 
      const a = [1, 2, 3];
      a.forEach(getSum);
      console.log(sum);
      function getSum(ele) {
         sum += ele;
      }
      

      Answer : 6 , The above code snippet calculates the sum of numbers in an array, by using the forEach loops method.

      7) What will be the output of the following code snippet?

      let n = 24;
      let l = 0, r = 100, ans = n;
      while(l <= r) {
         let mid = Math.floor((l + r) / 2);
         if(mid * mid <= n) {
             ans = mid;
             l = mid + 1;
         }
         else {
             r = mid - 1;
         }
      }
      console.log(ans);

      The code snippet basically uses binary search to calculate the floor of the square root of a number. Since the square root is an increasing function, so binary search is applicable here. Here, for n = 24, the answer is 4.

      8) What will be the output of the following code snippet?

      (function(a){
          console.log(a);
       (function(){
         console.log(a);
         (function(b){
             console.log(b);
         })(100)
         a = 6;
       })()
      })(50);

      Even though a is defined in the outer function, due to closure, inner functions have access to it.

      9) What will be the output of the following code snippet?

      print(NaN === NaN);

      In Javascript, NaN is not considered to be equal to NaN even after using the strict equality operator. Answer is false

      10) What will be the output of the following code snippet?

      function dog() {
         print("I am a dog.");
      }
      dog.sound = "Bark";

      Nothing happens in the above code, and it is totally valid because functions in Javascript are treated as objects.

      11) What will be the output of the following code snippet?

      var a = "Scaler";
      var result = a.substring(2, 4);
      document.write(result);

      The substring function in javascript slices a substring out of a given string from the start to end indexes(excluding the end index). So the 2nd and 3rd characters are taken here(0-based indexing) and the answer is al. substring(<index>,<length of the given string starts from 0 index>), index starts from 0 aiwyas.

      12) What will be the output of the following code snippet?

      (function(){
       setTimeout(()=> console.log(1),2000);
       console.log(2);
       setTimeout(()=> console.log(3),0);
       console.log(4);
      })();

      First the 2 is printed with the console.log, then even with a time delay of 0ms, the 4 is printed before the 3 because JS executes setTimeout with the Web API, and so the entire function is executed first. Lastly, after a delay of 2000ms, the 1 is printed.

      13) What will be the output of the following code snippet?

      var a = 1;  
      var b = 0;  
      while (a <= 3)  
      {  
         a++;  
         b += a * 2;  
         console.log(b);
      }

      The loop will run 3 times, before meeting the exit condition. First value of b will be 2 * 2 = 4, followed by 4 + 3 * 2 = 10, and then value of 10 + 4 * 2 = 18.

      14) What will be the output of the following code snippet?

      a = [1, 2, 3, 4, 5];
      console.log(a.slice(2, 4));

      The slice() function in Javascript slices an array within the given start and end indexes and then returns the values lying in those ranges. The indexing done is 0-based indexing.

      15) What will be the output of the following code snippet?

      var a = true + true + true * 3;
      console.log(a)

      In Javascript, true is considered as the number 1 when used in any arithmetic expression, hence the expression evaluates to 5.

      16) What will be the output of the following code snippet?

      function test(...args) {
       console.log(typeof args);
      }
      test(12);

      The …args parameter allows us to collect all remaining arguments into an array, and in Javascript typeof an array is an object.

      17) What will be the output of the following code snippet?

      var a = "hello";
      var sum = 0;
      for(var i = 0; i < a.length; i++) {
         sum += (a[i] - 'a');
      }
      console.log(sum);

      In Javascript, the a[i] – ‘a’ is not typecasted to an integer type and hence the result is NaN.

      18) What will be the output of the following code snippet?

      const set = new Set();
      set.add(5);
      set.add('Hello');
      set.add({ name: 'Scaler' });
      for (let item of set) {
       console.log(item + 6);
      }

      First 2 numbers are added as integers, followed by string concatenation. Finally, since both are not of string type, JS stringifies both the object and the number and concatenates them. When an object is stringified it is read as [object Object] and then concatenated.

      19) What will be the output of the following code snippet?

      function solve(arr, rotations){
       if(rotations == 0) return arr;
       for(let i = 0; i < rotations; i++){
         let element = arr.pop();
         arr.unshift(element);
       }
       return arr;
      }
      // solve([44, 1, 22, 111], 5);

      [ 111, 44, 1, 22 ]

      20) What will be the output of the following code snippet?

      const example = ({ a, b, c }) => {
       console.log(a, b, c);
      };
      example(0, 1, 2);

      Since we are passing individual numbers rather than a single object to the function, Javascript will initialize the object parameters with their default value of undefined.