JavaScript Interview Questions

Q1: What are the ways to involve JavaScript code in an HTML file?

Ans: To involve a JavaScript code in an HTML file, we have three ways in total. These ways are written below:

  • Inline
  • Internal
  • External

Let’s begin with the inline function. It is a JavaScript function, to a variable generated at runtime, this function is dedicated. As it is assigned to a variable, you can use it again. That’s how you can differentiate between Inline Functions and Anonymous. If you require JavaScript for a function, you can work using two methods. You can integrate the script into the page that you are building up. Another way is to put it in a separate file. In this way, you can use it whenever you want. That is the differentiation between an internal script and an external script.

Q2: How to describe a variable in JavaScript?

Ans: Surprisingly, to define the variable in JavaScript, again you have three ways. These are briefly described in the next few lines.

  • Var – the variables statement in JavaSCript is used to mention a variable. Sometimes, you can set the value of that variable. For instance: var a =10; prior to the implementation of the code, it processes the variable declarations.
  • Const – The notion of const functions does not permit them to make modifications to the object on which you call them. When we call a function const, we are able to call it on any type of object.
  • Let – It is basically a signal that the variable may assign to a new position. Consider the example of a counter in a loop, swapping of the value in an algorithm. And, this signals that you can use the variable in the block where it is defined.

Q3: What do you understand by Typed language?

Ans: In JavaScript, you don’t have to set out the information you are going to store in a variable. That is because it is a loosely typed language. JavaScript types a variable depending on the type of information you assign to it on its own. There are several languages that resemble Java, and won’t require you to specify a variable’s type like int, float, boolean, or String.

In Typed Language, the values are related to the values instead of variables. It has two types:

  • Dynamically: this type enables the variable to take several types. In JS, a variable can hold numbers, chars.
  • Statically: this type lets the variable hold only one type. A variable specified of a string is allowed to take a single set of characters.

Q4: Tell the difference between Local storage & Session storage?

Ans: When we talk about local storage, the data doesn’t return to the server for every HTTP request. Consequently, the amount of traffic between client and server decreases. It is not removed till you clear via setting or program manually.

Session Storage resembles local storage in many ways. They have the same API. both f these are used to save keys or value pairs. However, there is one difference. The data stored in the local storage doesn’t expire, whereas, in session storage, data is removed when the page session ends. Session Storage will leave when the browser is closed.

Q5: Differentiate between these two operators ‘==‘ & ‘===‘?

Ans: “==” operator with the help of type correction compares the variables. It allows you to compare numeric information with string, etc. The “===” doesn’t allow that. That is due to the way it functions. It checks the value and the type of two variables. if two variables are unidentical “===” return false. For this operation “==” return true.

JavaScript is known to be strict. So, it's essential to understand which operator is suitable for the type of operation you want to perform. For the comparison of two variables of a different type, for example, a boolean with a string or a vice versa using == operator. In this situation, it changes one type into another automatically. And, it returns the value depending on content equality. On the other hand, if the two variables are of the same type and contain the same value, === operator returns true.