JavaScript Question Bank - Detailed Answer Sheet
1. List any four features of JavaScript.
JavaScript is one of the most widely used programming languages in web development. Some of its
key features are:
1. Lightweight and interpreted: JavaScript code runs directly in the browser without compilation.
2. Object-Oriented: Supports objects, inheritance, and prototypes.
3. Event-driven: Enables interactive web applications by handling user events like clicks, inputs,
etc.
4. Platform-independent: Runs across multiple browsers and operating systems.
5. Asynchronous programming support: Provides callbacks, promises, and async/await for handling
asynchronous tasks.
2. List & explain datatypes in JavaScript.
JavaScript supports both primitive and reference datatypes:
Primitive Types:
- String: Represents text data. Example: 'Hello'.
- Number: Represents numeric values (both integer and float). Example: 42, 3.14.
- Boolean: Represents true/false values.
- Null: Represents an empty or null value.
- Undefined: A variable declared but not assigned has 'undefined'.
- Symbol: Represents unique identifiers.
- BigInt: Represents very large integers beyond Number limits.
Non-Primitive (Reference Types):
- Object: Collection of key-value pairs.
- Array: Ordered collection of values.
- Function: Reusable block of code.
- Date, RegExp, Map, Set, etc.
3. Write a JavaScript program to check whether entered number is prime or not.
A prime number is divisible only by 1 and itself. Below is the program:
function isPrime(num) {
if (num <= 1) return false;
for (let i = 2; i <= Math.sqrt(num); i++) {
if (num % i === 0) return false;
}
return true;
}
console.log(isPrime(7)); // true
Here, we check divisibility up to square root of the number for efficiency.
4. Difference between == and === operators.
- == (Equality Operator): Compares values after type conversion (type coercion). Example: 5 == '5'
→ true.
- === (Strict Equality Operator): Compares values and types strictly, without conversion.
Example: 5 === '5' → false.
5. Difference between var and let keyword in JavaScript.
- var: Function-scoped, hoisted to the top, allows redeclaration.
- let: Block-scoped, not accessible outside its block, no redeclaration allowed.
Example:
if(true){ var x=10; let y=20; }
console.log(x); //10
console.log(y); //Error
6. Explain implicit type coercion in JavaScript.
Implicit type coercion occurs when JavaScript automatically converts one data type to another
during operations.
Examples:
- '5' + 2 → '52' (number converted to string).
- '5' - 2 → 3 (string converted to number).
- true + 1 → 2 (boolean converted to number).
7. What is NaN property in JavaScript?
NaN stands for 'Not-a-Number'. It represents an invalid number result.
Examples:
- parseInt('abc') → NaN
- 0/0 → NaN
- typeof NaN → 'number'
8. Output of given code.
let y = 30;
if (true) { let y = 40; console.log(y); }
console.log(y);
Output:
40
30
Explanation: Inner 'let y=40' shadows the outer variable inside the block.
9. Output of given code.
let a = 5;
function test() { let a = 10; console.log(a); }
test();
console.log(a);
Output:
10
5
Explanation: Local variable 'a' inside function shadows outer variable.
10. Which keyword is used to declare a variable that cannot be reassigned?
The const keyword is used to declare constants. Its value cannot be reassigned after initialization.
11. Output of given code.
let x = 'Hello';
let y = 5;
console.log(x + y);
Output:
Hello5
Explanation: String concatenation occurs as 'x' is a string.
12. Output of given code.
let x;
console.log(typeof x);
Output:
undefined
Explanation: Declared variable without assignment has type 'undefined'.
13. Output of given code.
console.log(5 + 5 + '5');
Output:
105
Explanation: (5+5)=10 first, then concatenated with string '5' → '105'.
14. Output of ternary operation.
const value = 10;
console.log(value > 5 ? 'Yes' : 'No');
Output:
Yes
Explanation: Condition is true, hence 'Yes'.
15. Output of given code.
console.log(5 == '5');
Output:
true
Explanation: '==' performs type coercion, so string '5' is converted to number 5.