1. Variables in JavaScript
In JavaScript, variables are used to store data that can be used and manipulated throughout your program. JavaScript allows you to declare variables using var
, let
, and const
.
Declaring Variables
There are three main ways to declare variables in JavaScript:
var x = 10; // Declares a variable globally or function-scoped
let y = 20; // Block-scoped, preferred for variables that may change
const z = 30; // Block-scoped, used for constants (cannot be reassigned)
- var is function-scoped and can be redeclared and updated. It is hoisted, meaning it is moved to the top of its scope during execution.
- let is block-scoped and allows you to declare variables that can be reassigned. It is not hoisted in the same way as
var
. - const is also block-scoped but must be assigned a value at the time of declaration and cannot be reassigned.
Differences Between var, let, and const
Keyword | Scope | Reassignable | Hoisted | Use Case |
---|---|---|---|---|
var | Function | Yes | Yes (but initialized as undefined) | Legacy code; avoid using in modern JavaScript |
let | Block | Yes | No (in temporal dead zone until declared) | Use when value will change later |
const | Block | No | No | Use for constants or fixed values |
Examples
// Using let
let age = 25;
age = 26; // valid
// Using const
const pi = 3.14159;
pi = 3.14; // Error: Assignment to constant variable
// Using var
function example() {
if (true) {
var test = "hello";
}
console.log(test); // works due to function scope
}
JavaScript: Variables and Data Types
1. Which keyword is used to declare a variable that cannot be reassigned in JavaScript?
2. Which of the following is a primitive data type in JavaScript?
3. What will be the output of the following code?
var x = 10;
var x = 20;
console.log(x);
4. Which operator is used to check the data type of a variable in JavaScript?
5. Which of the following is the correct way to declare a string variable in JavaScript?
6. What will be the output of the following code?
let result = 3 + '5';
console.log(result);
7. Which data type can represent very large integers in JavaScript?
8. What will happen when the following code is executed?
const num = 5;
num = 10;
console.log(num);
9. What is the difference between 'null' and 'undefined' in JavaScript?
10. What will be the output of the following code?
let value = null;
console.log(typeof value);
11. Which of the following keywords is block-scoped in JavaScript?
12. Which data type was introduced in ES6 to create unique values?
13. What will be the output of the following code?
let a = 10;
let b = '10';
console.log(a + b);
14. What does 'NaN' stand for in JavaScript?
15. What is the initial value of a variable that has been declared but not yet assigned a value in JavaScript?
16. What will be the output of the following code?
let y = 10;
if (true) {
let y = 20;
}
console.log(y);
17. Which of the following declarations allows a variable to be accessible outside of a block?
18. Which statement is true about 'null' and 'undefined'?
19. What happens if you try to declare a 'const' variable without initializing it?
const value;
console.log(value);
20. Which data type in JavaScript can store large integers beyond the safe integer limit?