JavaScript Interview Questions

 12 JavaScript Interview Questions



 1) What is the difference between the operators '= =' and '= = =' ?

  = = compares two variables irrespective of data type while = = = compares two variables in a strict check.

Example : document.write(9 = = "9") ;     // true                                                                                    document.write(9 = = = "9") ;  //false

 

 2) How to Empty an Array in JavaScript ?

This code will clear the existing array by setting its length to 0.

Example : arrayList.length = 0;


 3) What is a higher-order function ?

 Higher-order function is a function that accepts another function as an argument or return value.

Example : const firstOrderFunc = ( ) => console.log ( 'Hello I am a First order Function' );                     const higherOrder = ReturnFirstOrderFunc =>ReturnFirstOrderFunc ( );                                higherOrder (firstOrderFunc);


  4) Difference between let and var ?

The main difference is the scope difference, while let can be only available inside the scope it's declared, like in for loop , var can be accessed outside the loop for example.


  5) What are closures ?

It is an inner function that has a access to the outer or enclosing function's variables.

Example : function Welcome (name) {

                     var greetingInfo = function (message) {

                  console.log (message + '  ' + name);

            }

      return greetingInfo;

        }

     var myFunction = Welcome (' John ');

      myFunction( 'Welcome ');   // Output : Welcome John

    myFunction( 'Hello Mr.' );   // output : Hello Mr. John 

 

6) What is the purpose JSON stringify ?

  When sending data to a web server , the data has to be in a string format.

  var userJSON =  { 'name' : 'John' , age: 31 }

  var userString = JSON.stringfy(user);

  console.log(userString);   // " {"name":"John","age":31}"


  7) What are JavaScript Data Types ?

Following are the JavaScript Data Types :

  1. Number
  2. String
  3. Boolean
  4. Object
  5. Undefined

 8) What would be the result of 3+2+"7"

Since 3 and 2 are integers , they will be added numerically. And since 7 is a string , its concatenation will be done. So the result would be 57.


 9) List out the different ways an HTML element can be accessed in a JavaScript code.

Here are the list of ways an HTML element can be accessed in a JavaScript code:

1 document.getElementById( 'idname' ) // Gets an element by its ID name

2 document.getElementByClass( 'classment' ) // Gets all the elements that the given classname.

3 document.getElementByTagName( 'tagmname' ) // Gets all the element that have the given tag name

4 document.querySelector( ) // This function takes css style selected and returns the first selected element.


 10) Difference between let and var ?

The difference between them is that var is function scoped and let is block scoped.

It can be said that a variable declared with var is defined throughout the program as compared to let.


 11) What is Hosting ?

Hosting is a JavaScript mechanism where variables and function declarations are moved to the top of their scope before code execution.

Example : 

    console.log(message); // output : undefined

    var message = 'The variable Has been hoisted' ;

    // The above code looks like as below to the interpreter

    var message ;

    console.log(message);

    message = 'The variable Has been hoisted';


12) How do you copy properties from one object to other

You can use the Object.assign() method

const target = { a:  1, b:  2 };

const source = { b:  3, c:  4 };

const returnedTarget = object.assign(target, source);

console.log(target);   // { a:  1, b:  3, c:  4} 

console.log(returnedTarget);   // { a:  1, b:  3, c:  4} 

Comments

Popular posts from this blog

Python Program to Generate Password

How to become a Hacker ?