Link Search Menu Expand Document

JavaScript Variables Interview Questions

JavaScript (or JS) variables interview questions contain the most frequently asked questions about JavaScript, which can help you crack any JS interviews. Here, we have focused on JS variables.

Q1. What are variables?

Answer: Variables are named memory allocations where you can store different types of data.

Q2. What are the basic data types present in JavaScript?

Answer: There are eight basic data types in JavaScript. They are:

  1. Primitive Data Types
  2. String
  3. Number
  4. Boolean
  5. Composite Data Types
  6. Object
  7. Array
  8. Function
  9. Special Data Types
  10. Undefined
  11. NULL

Q3. Define undeclared and undefined variables.

Answer: Undeclared variables are those which are not declared and do not exist in any program. You will face a runtime error if you try to read any undeclared variables.

Undefined variables are named memory allocations which are already declared, but no value has been assigned to that memory allocation. If you try to read the value of an undefined variable, the program will run, but it will return an undefined value.

Q4. What are global variables, and how can you declare them?

Answer: Global variables are memory allocations that can be used throughout the program (or you can say it has a global scope). In JavaScript, you can use the var keyword to declare any local variable or object. If you omit the var keyword, you can declare a global variable.

Q5. What are the disadvantages of using global variables?

Answer: There are two primary disadvantages of using global variables.

  1. If you use a local and a global variable with the same name, you may experience some clash between them.
  2. If you rely on global variables, it becomes harder to test the code and find out any bugs.

Q6. What is the isNaN function?

Answer: The isNaN() function returns true (Boolean 1) if the value of the variable sent in the argument is not a number.

Q7. What is this keyword in JavaScript?

Answer: The “this” keyword in JavaScript is used as a reference variable to point to the current object.

Q8. How can you create an array in JavaScript?

Answer: You can create an array in JavaScript in the following ways:

  1. By using array literals.
  2. You can create an instance of an array.
  3. Use an array constructor.

Q9. How can you declare an Object in JavaScript?

Answer: You can declare an object in JS in the following ways:

  1. By using object literals.
  2. Create an instance of an object.
  3. By using object constructor

Q10. When do we use variable typing in JS?

Answer: We use variable typing in JS when we want to assign a number to a variable. You can assign the same variable to a string.

Q11. State the difference between call by value and call by reference.

Answer: In call by value, we send a copy of the data present in the actual argument to the formal argument when we call the function. Any changes made within that function will not affect the variables that we sent as actual arguments.

In the call-by-reference method, we send reference variables as arguments that point to variables that will be used in the function. Any changes made within the function will affect the original variables to which the reference variables were pointing.

Other useful articles:


Back to top

© , Study JavaScript — All Rights Reserved - Terms of Use - Privacy Policy