JavaScript has come a long way from the place where it was just a scripting language used for basic validations or form processing to a very advanced user interactions and complex web page processing.

I am personally a very big fan of JavaScript language because its awesome. So thought of listing down some interview questions for those who are interested in cracking JavaScript interviews. If you have a better explanation or answer for following JavaScript Interview Questions then do let me know. Here are JavaScript interview questions and answers.

If you are a JavaScript UI Developer or JavaScript Frontend Developer and looking for some JavaScript interview questions then keep reading JavaScript Interview Question Answers. I will be posting some more Basic and Advanced JavaScript interview questions with details for beginners and experienced JavaScript developer. If you have any suggestions then let me know.

UI Developer JavaScript Interview Question Answers
JavaScript Interview Question Answers Latest
  1. Is JavaScript Case-Sensitive ?

    • Yes. JavaScript is Case-Sensitive.
    • var myText = 'sample';
      

    • var MyText = 'Sample';
      

    • Above two codes creates 2 different variables.
  2. What are the Data Type supported by JavaScript ?

    • String
    • Number
    • Boolean
    • Array
    • Object
    • Null
    • Undefined
  3. What is the difference between double equal to ‘==’ and triple equal to ‘===’ ?

    • Double Equal to checks if left hand side is equal to right hand side. eg.
    • var count = 7;
      if(count == 7) {} //this checks if count is equal to 7
      
    • Triple Equal to check if left hand side is equal to right hand side and data type of left hand side is equal to right side. In the following example we are checking that 7 is equal to “7” and datatype of both is same. But in the following example 7===”7″ will return false because left hand side item is Number 7 and right hand side is String “7”.
    • if(7 === "7") {} // this will return false
      
  4. How can you execute any function at specific time or in specific intervals in JavaScript ? Or What are timing events functions in JavaScript ? Or what are timers in JavaScript ?

    • In JavaScript there are 2 functions with which you can execute functions at specified time.
    • setTimeout() function
      • It executes given function after specified time has elapsed.
      • First parameter should be a function. If its not a function then you will get unexpected results.
      • You can write setTimeOut() function as window.setTimeout() also.
      • Second parameter is milliseconds.
      • This function is executed only once after specified milliseconds are over.
      • You can store this function inside a variable and then you can use clearTimeout() method by passing variable name as parameter, to stop execution of the function. This can be useful sometimes eg. After some processing you can show a popup stating that it will get closed in 5 seconds (5000 milliseconds) and you can give an option to keep the popup open.
    • setInterval() function
      • It executes given function continuously after specified time.
      • First parameter should be a function. If its not a function then you will get unexpected results.
      • You can write setInterval() function as window.setInterval() also.
      • Second parameter is milliseconds.
      • This function gets executed continuously after specified time is over.
      • If you want to stop execution of this function then you need to store this function inside a variable and then use the clearInterval() method by passing variable name as parameter. This can be useful sometimes eg. If you are showing a countdown timer and want to give a facility to user to stop that timer.
  5. How can we handle errors or exceptions in JavaScript ?

    • We can use try catch block  to handle errors.
    • We can send custom error messages with the help of throw.
    • Use try {} block around your code.
    • Send custom error messages with the help of throw “error message”.
    • Put catch(err) {} block after that. And use the err variable inside catch block to display custom error message to the user with custom display.
    • You can use finally {} block after catch(err) {} block to execute some default code always.
    • You can use try catch throw finally everywhere eg. when handling form data.
  6. How to find number of parameters passed to a function ? Or How to find number of arguments passed to a function ?

    • Every function in JavaScript has an inbuilt object called “arguments”.
    • “arguments” object contains all the parameters passed to a function.
    • You can find how many parameters are passed to function by check “arguments.length”.
    • You can access any argument/parameter by passing an index. eg. arguments[0]
    • If you pass a non-existent index then it will return “undefined”.
  7. How to submit a form using JavaScript ?

    • Considering that you have one form on a page then you can use following to submit a form.
    • document.form[0].submit();

    • If you have multiple forms on a page then you can refer that form with id and then submit it. See example below
    • document.getElementById("registrationForm").submit();

  8. How to add a new css class to any existing html element ? Or How to append a new css class to a div ?

    • You can add new css to any existing html element by finding that element and concatenating new css classname with the existing css classnames.
    • document.getElementById("myDiv").className = document.getElementById("myDiv").className + " newClassName"; 

    • Even in above string concatenation you can use “+=” for concatenating new css classname to the existing css classnames.
  9. What is delete operator in JavaScript ?

  10. What is that “void(0)” written at some places ?

If you find these JavaScript Interview Questions and Answers for Freshers then do share it with your friends. If you think that JavaScript Interview Questions and Answers are useful for Experienced JavaScript UI Developer or for Experienced JavaScript Frontend Developer then do share it with your friends.

 

If you are looking for some more useful interview questions then do have a look at “25 Essential JavaScript Interview Questions