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?
21. What will be the output of the following code?
let result = '10' - '5';
console.log(result);
22. Which syntax is used for template literals in JavaScript?
23. What will be the output of the following code?
console.log(x);
var x = 5;
24. How many primitive data types are there in JavaScript (ES2020)?
25. What will happen when the following code is executed?
console.log(y);
let y = 10;
26. Which method can be used to convert a value to a boolean in JavaScript?
27. What will be the output of the following code?
console.log(5 == '5');
console.log(5 === '5');
28. How do you create a BigInt literal in JavaScript?
29. What will be the output of the following code?
let str = '42';
let num = +str;
console.log(typeof num);
30. What is true about Symbol data type in JavaScript?
31. What will be the output of the following code?
var z = 30;
if (true) {
var z = 40;
}
console.log(z);
32. Which function is used to convert a string to an integer in JavaScript?
33. What will be the output of the following code?
let result = 0 / 0;
console.log(result === NaN);
34. What is the Temporal Dead Zone in JavaScript?
35. What will be the output of the following code?
console.log('3' * '4');
36. Which function is used to check if a value is NaN in JavaScript?
37. What will be the output of the following code?
const obj = { name: 'John' };
obj.name = 'Jane';
console.log(obj.name);
38. What is the maximum safe integer value in JavaScript?
39. What will be the output of the following code?
console.log(0 && 'Hello');
console.log('World' && 42);
40. Which method converts any value to a string in JavaScript?
41. What will be the output of the following code?
console.log(null || 'default');
console.log('' || 0 || 'fallback');
42. What is the scope of a variable declared with 'var' inside a function?
43. What will be the output of the following code?
console.log(parseFloat('3.14abc'));
44. Which method checks if a value is an integer in JavaScript?
45. What will be the output of the following code?
let age = 20;
let status = age >= 18 ? 'adult' : 'minor';
console.log(status);
46. Which method checks if a number is finite in JavaScript?
47. What will be the output of the following code?
console.log(void 0);
console.log(void 'hello');
48. What happens when you declare a variable without using var, let, or const?
49. What will be the output of the following code?
console.log(!!'');
console.log(!!'hello');
console.log(!!0);
50. Which method provides the most accurate way to compare two values in JavaScript?