Basic JavaScript - Interview Questions and Answers

JavaScript is a client-side as well as server side scripting language that can be inserted into HTML pages and is understood by web browsers. JavaScript is also an Object based Programming language.

  • Undefined is the default value of a variable that has not been assigned a specific value. Or a function that has no explicit return value ex. console.log(1). Or a property that does not exist in an object. The JavaScript engine does this for us the assigning of undefined value.
  • Null is "a value that represents no value"null is value that has been explicitly defined to a variable. In this example we get a value of null when the fs.readFile method does not throw an error.
  • When comparing null and undefined we get true when using == and false when using ===
  • console.log(null == undefined); // logs true
    console.log(null === undefined); // logs false

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

Yes, JavaScript is a case sensitive language.  The language keywords, variables, function names, and any other identifiers must always be typed with a consistent capitalization of letters.

Yes, An anonymous function can be assigned to a variable. It can also be passed as an argument to another function.

The JavaScript this keyword refers to the object it belongs to. This has different values depending on where it is used. In a method, this refers to the owner object and in a function, this refers to the global object.

The JavaScript this keyword refers to the object it belongs to. This has different values depending on where it is used. In a method, this refers to the owner object and in a function, this refers to the global object.

  • 'ViewState' is specific to a page in a session.
  • 'SessionState' is specific to user specific data that can be accessed across all pages in the web application.

We use the ‘JavaScript Hoisting’ method, when an interpreter runs the code, all the variables are hoisted to the top of the original /current scope. If you have a variable declared anywhere inside the code, then it is brought to the top.

This method is only applicable to the declaration of a variable and is not applicable for the initialization of a variable. Functions are also hoisted to the top, whereas function explanations are not hoisted to the top.

Netscape provided the JavaScript language. Microsoft changed the name and called it JScript to avoid the trademark issue. In other words, you can say JScript is the same as JavaScript, but Microsoft provides it.

Two types of comments in JavaScript.

  1. Single Line Comment: It is represented by // (double forward slash)
  2. Multi-Line Comment: Slash represents it with asterisk symbol as /* write comment here */

Breaking within a string statement can be done by the use of a backslash, '\', at the end of the first line.

Example: 

document.write("This is \a program");

JavaScript is a dynamically typed language. In a dynamically typed language, the type of a variable is checked during run-time in contrast to statically typed language, where the type of a variable is checked during compile-time.

Three ways of defining a variable in JavaScript are:

  • Var – The JavaScript variables statement is used to declare a variable and, optionally, we can initialize the value of that variable. Example: var a =10; Variable declarations are processed before the execution of the code.
  • Const – The idea of const functions is not allow them to modify the object on which they are called. When a function is declared as const, it can be called on any type of object.
  • Let – It is a signal that the variable may be reassigned, such as a counter in a loop, or a value swap in an algorithm. It also signals that the variable will be used only in the block it’s defined in.

innerHTML – It will process an HTML tag if found in a string

innerText – It will not process an HTML tag if found in a string

The 2 and 5 are integers, they will be added numerically. And since 3 is a string, its concatenation will be done. So the result would be 73. The ” ” makes all the difference here and represents 3 as a string and not a number.

In order to detect the operating system on the client machine, the navigator.platform string (property) should be used.

  • Alert
  • Confirm 
  • Prompt

An alert box displays only one button which is the OK button. But a Confirmation box displays two buttons namely OK and cancel.

document.write("Hello") is used to print the text – Hello in the screen.

Share   Share