JavaScript Interview Questions

What is JavaScript?, What are the features of JavaScript ?, Is JavaScript a case-sensitive language?, How can you create an object in JavaScript?
             What are the advantages of JavaScript?, What are the disadvantages of JavaScript?, How can we read the properties of an object in JavaScript?,What are JavaScript Data Types?

1. What is JavaScript?

JavaScript is a client-side and server-side scripting language that can be written into HTML pages and it is supported by web browsers. JavaScript is most preferred scripting language for client-side or server-side. JavaScript is an interoperated programming language which supports object-oriented features.

2. What are the features of JavaScript ?

JavaScript support following features :

  • It is lightweight
  • Interpreted programming languages
  • Created for network-centric applications
  • Supports object-oriented programming
  • Platform independent
  • Open source

3. Is JavaScript a case-sensitive language?

Yes, JavaScript is a case-sensitive programming language that means you have declared variable name, functions, const name then use the same name to access them.
For example

  

        var userName ='John Doe'
        console.log(userName);  // output : John Doe
        console.log(username); // Error username is not defined
    

4. How can you create an object in JavaScript?

You can create JavaScript Object using following ways

A. Using object literals

  

        var employeeObj ={
        empCode:'',
        empName:'',
        printDetail:function(){
        console.log(`  Code :${this.empCode}     Name : ${this.empName}`);
        }
        }

        employeeObj.empCode='E0001';
        employeeObj.empName ='John Doe';
        employeeObj.printDetail();  //   Code :E0001     Name : John Doe
    

In the above code snippet create a employeeObj(empCode and empName) with two properties and one function(printDetail)

B. Using new keyword/ object instance

  

        var employeeObj = new Object();

        employeeObj.empCode='E0001';
        employeeObj.empName ='John Doe';

        console.log(`  Code ${employeeObj.empCode}     Name : ${employeeObj.empName}`);  // Employee Code :E0001   Employee Name : John Doe
    

C. Using Object.create() function

  

        var Employee ={
        empCode:'',
        empName:'',
        printDetail:function(){
        console.log(`  Code :${this.empCode}     Name : ${this.empName}`);
        }
        }

        var employeeObj = Object.create(Employee);

        employeeObj.empCode='E0001';
        employeeObj.empName ='John Doe';

        console.log(`  Code :${employeeObj.empCode}     Name : ${employeeObj.empName}`);  //   Code :E0001     Name : John Doe
    

D. Using function

  

        function createEmployeeObject(empCode, empName)
        {
        this.empCode = empCode;
        this.empName= empName;

        this.printDetail= function(){
        console.log(`Code :${this.empCode}  and  Name : ${this.empName}`);
        }
        }

        var employeeObj = new createEmployeeObject('E0001','John Doe');
        employeeObj.printDetail()  //  Code :E0001     Name : John Doe
    

E. Using Single function

  

        var employeeObj  = new function ()
        {
        this.empCode='' ;
        this.empName='';

        this.printDetail= function(){
        console.log(`Code :${this.empCode} and   Name : ${this.empName}`);
        }
        }

        employeeObj.empCode='E0001';
        employeeObj.empName ='John Doe';

        employeeObj.printDetail()  //  Code :E0001     Name : John Doe

    

5. What are the advantages of JavaScript?

Advantage of JavaScript as follows :

  • Less Sever interaction
  • quick feedback to the visitors
  • High Interactivity
  • Richer Interfaces
  • Separation of Code

6. What are the disadvantages of JavaScript?

Disadvantage of JavaScript as follows :

  • Multithreading No support
  • Multiprocessing No support
  • Cannot Reading and writing files
  • Different browsers interpreted differently  sometimes
  • Less secure due to client side secure
  • Stopped working if JavaScript is disabled
  • Difficult to debug

7. How can we read the properties of an object in JavaScript?

You can use read/access the properties of object using dot(.)properties name.

Syntax : ObjectName.PropertyName

  

        var employeeObj ={
        empCode:'',
        empName:'',
        printDetail:function(){
        console.log(`Employee Code :${this.empCode}   Employee Name : ${this.empName}`);
        }
        }

        employeeObj.empCode='E0001'; // set the empcode property value
        employeeObj.empName ='John Doe';
        employeeObj.printDetail();  //  access the object function
    

8. What are JavaScript Data Types?

JavaScript categories the data type into two categories:

Primitive Data Type

  • string
  • number
  • boolean
  • undefined
  • null

Non-Primitive Data Type

  • Object
  • Array
  • RegExp

9. how many types of function in JavaScript?

There are two type of functions on JavaScript
A) Name function
B) Anonymous Function

10. What is name function on JavaScript ?

A function is declared with name is called the named function.
Example :

  

        function namedFunction(){
        console.log(`name function`);  // name function
        }
        namedFunction();
    

10. What is Anonymous function on JavaScript ?

A function does not contain the name, called Anonymous functions.
Example

  

        var anonymousFunction =function(){
        console.log(`anonymous function`); //anonymous function
        }

        anonymousFunction();
    

11.Can you assign an anonymous function to a variable and pass it as an argument to another named function?

Yes, you can assign the anonymous function to a variable and pass it as function argument. See the below code snippet to pass it as a function argument

  

        // create an anonymous function
        var anonymousFunction =function(){
        console.log(`anonymous function`); //anonymous function
        }

        // create a function which take the anonymous function as argument  and call the  anonymous function inside function
        function functionTakeAnonymousArgument(functionName){

        functionName();
        }

        // pass the anonymousFunction as argument
        anonymousFunctionArg(anonymousFunction);    // output :  anonymous function

    

12. Can an anonymous function be assigned to a variable?

Yes. You can assign the anonymous function to a variable like below code snippet.

  

        // create a anonymous function
        var anonymousFunction =function(){
        console.log(`anonymous function`);
        }
    

13. What is arguments objects in JavaScript?

Arguments object is a special construct available withing function all functions call. It represents the list all the arguments/parameters passed while invoking the functions. It’s just like array.
Example

  

        function testArguments(params) {

        console.log(arguments);
        console.log(`Total number of parameters passed ${arguments.length}`);


        const args = Array.from(arguments);

        args.forEach((param,index) => {

        console.log(`${param} parameter passed at ${index} index.`);
        });
        }

        testArguments(101);
        // output :
        // Arguments] { '0': 101 }
        // Total number of parameters passed 1

        testArguments(101,'John Doe',32)
        //Output :
        // [Arguments] { '0': 101, '1': 'John Doe', '2': 32 }
        // Total number of parameters passed 3
        // 101 parameter passed at 0 index.
        // John Doe parameter passed at 1 index.
        // 32 parameter passed at 2 index.

    

14. how to get the type of arguments passed to a function?

Use the typeof type to check the data type of passed arguments

  

        function testArguments(params) {

        const args = Array.from(arguments);

        args.forEach((param,index) => {

        let datatype= (typeof param)
        console.log(`${param} parameter is ${datatype} type.`);
        });
        }

        testArguments(101);

        //101 parameter is number type.

        testArguments(101,'John Doe',32,{name:'John Doe'})

        // 101 parameter is number type.
        // John Doe parameter is string type.
        // 32 parameter is number type.
        // [object Object] parameter is object type.
    

15. How many types of variable what are the scope of variables in JavaScript? What are the scopes of a variable in JavaScript?

There are two type of variables declaration support by JavaScript and these are as follows

  • Global variable
  • Scope/local variable

16. What is the difference between global and scope/local variables in JavaScript?

  • Global Variables : a variable declared as global variable can be accessible everywhere.
  • Local Variables : a local variable declared within block/function and accessible only that block/functions
  

        var globalVariable ='Global Variable ';

        function testLocalVariable(params) {
        var localVariable ='Local Variable'
        globalVariable='value changed within function';
        console.log(localVariable);
        console.log(globalVariable);
        }

        console.log(globalVariable);   //Global Variable
        testLocalVariable();
        //Local Variable
        //value changed within function
        console.log(globalVariable);  //value changed within function

    

If you try to access the local variable, then it will throw you the error message.

What is the difference between global and scope/local variables in JavaScript?

17. What is the use of this keyword in JavaScript?

JavaScript this keyword refers to the object that belong to and its value is depend on where it is using.

Some values are as follow based on where it is:

  • In a method it is refers to owner object
  • Alone, it is referred to global object
  • In strict mode function, it is undefined
  • In an event, it referred to the element that receive the event
  • Method like apply() and call() refer to any object

18. What is the use of isNaN function?

Its return true if passed argument/parameter is number otherwise return false.

19. What is negative infinity?

In JavaScript Negative Infinity is a number which can be derived by dividing negative number by zero.

  

        var num=-101;
        function displayInfinity()
        {
        console.log(num/0);
        }
        displayInfinity();
        // output: -Infinity
    

20. What is the difference between == and ===?

  • == : this operator values are equal.
  • === : this operator checks the value and variable data type are equal or not

21. Difference between “var” ,“let”, const Keywords?

  • var : it is function scoped variable and variable Hoisting is supported.
  • let : it is a block scoped variable and variable Hoisting is not supported.
  • const : cannot re-assign value to const variable

22. Difference between “undefine” and “NULL” Keywords?

  • Undefined : variable is declared but value is not assigned to it and undefined is typeof undefined.
  • Null : variable is declared with null value. Null is typeof object.

23. What is the output of 5+5+"10" in JavaScript?

1010 because first two values are numbers so do the arithmetic operator, after that do the string concatenate because “10” is string.

24. What is the output of "10"+5+5 in JavaScript?

1055 because expression start with string value so concatenate all the value

25. What are the pop-up boxes available in JavaScript?

  • Alert Box
  • Confirm Box
  • Prompt Box

26. What is non primitive data type?

Non-primitive values are mutable data types. The value of an object can be changed after it gets created.

They are also called "reference variables," or "object references," since they reference a memory location, which stores the data.