Javascript, which is abbreviated as JS, is a client-side, scripting language, which a front-end or full-stack developer uses to perform various functions on the client-side for better User experience and performance of web applications. So in this article, you will find the list of interview questions with answers related to Javascript which can be asked from JS developers.

js-interview-questions--and-answer

What is Javascript?

Javascript is a popular client-side scripting language, which can be inserted in HTML web pages to perform various functions like form validations, performing client-side calculations without page load, etc.

What are the advantages of Javascript?

Here are the advantages of using Javascript:

  • Make better UI
  • Server interaction is less or can be done without page load
  • Makes Interactivity higher
  • Lightweight
  • It is an interpreted language. Instructions are executed directly on the browser.
  • Supports object-oriented language.

What are different types of Javascript Data-types?

Following are the JavaScript Data types:

  • Number - For numeric values
  • String - For characters and alphanumeric values
  • Boolean - For true and false values
  • Object - For collections or complex values
  • Undefined - For variables that are only declared and not defined or initialized
  • NULL - For empty or unknown values.

What's the difference between undefined and null?

undefined is the default value of a variable that has not been assigned a specific value.

null is "a value that represents no value". null is a value that has been explicitly defined to a variable.

When comparing null and undefined we get true when using == and false when using ===, the reason for that is == compares by value after coercion and === compares by value and type without coercion.

coercion means the process of converting a value to another type

What is the use of isNaN function?

isNan is a in-built JS function which return true when a integer value is passed, false when value is not integer

What is the difference between undeclared and undefined variables?

undeclared variables are the ones which are not declared and used in JS code, at runtime you will get error in console for these variables.

undefined variables are ones, which has not been assigned a specific value.

Name type of functions available in Javascript?

There are two types of functions in Javascript, named functions and anonymous functions

  • Named functions: These type of functions contains name at the time of definition.
    Example:
    function addNumbers(){
       var a = 2;
       var b = 3;
       return (a+b); 
    }?
  • Anonymous functions: these type of function doesn't contain any name. They are declared dynamically at runtime.
        var helloWorld=function()  
        {  
          document.write("hello World from anonymous Function ");  
        }  
        helloWorld();  ?

Can an anonymous function be assigned to a variable?

Yes, you can assign anonymous functions to variables.

What is the difference between JScript and Javascript?

There isn't any difference, both are the same but Netscape invented Javascript and Microsoft relaunched it as JScript to avoid copyright/trademark issues.

Is Javascript a case-sensitive language?

Yes, Javascript is case-sensitive language, you have two variable with same name but with different casing, will make them store different values.

var msg = "Hello";
var Msg = "Hello world";
console.log(msg); // print "Hello"
console.log(Msg); // prints "Hello world"

What is the difference between && and || operator in Javascript?

&& is used to perform logical AND operation in JS, It employs short-circuiting to prevent unnecessary work, so if you use it in the "if" condition, which performs check for 2 conditions, then both of the conditions must be "TRUE" in order to execute code in "if" block.

|| performs logical OR, finds the first true expression, in "if" blocks, and executes the code inside it.

How to add an element dynamically using JS in HTML?

Here is a way to add h1 element dynamically in HTML, on buttons click

<html>
  <body>
    <button onclick="create()">Create Heading</button>
    <script>
      function create() {
        var h1 = document.createElement('h1');
        h1.textContent = "New Heading!!!";
        h1.setAttribute('class', 'note');
        document.body.appendChild(h1);
      }
    </script>
  </body>
</html>

In the above code, document.createElement is used with an HTML tag to create the element. The textContent is then modified and then the class attribute is modified using setAttribute.

What is difference between test() and exec() functions in JS?

By using a test(), we will search a string for a given pattern, if it finds the matching text then it returns the Boolean value "true" or else it returns "false’"

But in exec(), we will search a string for a given pattern (gives you an array-like return value with all capture groups and matched indexes), if it finds the matching text then it returns the pattern itself, or else it returns a "null" value.