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:
- Primitive Data Types
- String
- Number
- Boolean
- Composite Data Types
- Object
- Array
- Function
- Special Data Types
- Undefined
- 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.
- If you use a local and a global variable with the same name, you may experience some clash between them.
- 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:
- By using array literals.
- You can create an instance of an array.
- 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:
- By using object literals.
- Create an instance of an object.
- 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:
- General Interview Questions About JavaScript
- JavaScript Interview Questions For Beginners
- Complex Questions Asked in JavaScript Interview
- JavaScript Interview Questions for Advanced
- Challenge Yourself With These JavaScript Questions
- Most Asked JavaScript Coding Questions
- JavaScript Classes Interview Questions
- JavaScript Functions Interview Questions
- JavaScript Statements Interview Questions
- JavaScript Variables Interview Questions