[Go to site: main page, start]

0% found this document useful (0 votes)
14 views3 pages

JavaScript Question Bank Overview

The document provides a comprehensive overview of JavaScript, detailing its features, data types, and key programming concepts. It includes explanations of operators, variable declarations, type coercion, and examples of code outputs. Additionally, it covers specific programming tasks such as checking for prime numbers and using ternary operations.

Uploaded by

swiggyotp
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views3 pages

JavaScript Question Bank Overview

The document provides a comprehensive overview of JavaScript, detailing its features, data types, and key programming concepts. It includes explanations of operators, variable declarations, type coercion, and examples of code outputs. Additionally, it covers specific programming tasks such as checking for prime numbers and using ternary operations.

Uploaded by

swiggyotp
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

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 <= [Link](num); i++) {
if (num % i === 0) return false;
}
return true;
}
[Link](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; }
[Link](x); //10
[Link](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; [Link](y); }
[Link](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; [Link](a); }
test();
[Link](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;
[Link](x + y);
Output:
Hello5
Explanation: String concatenation occurs as 'x' is a string.

12. Output of given code.


let x;
[Link](typeof x);
Output:
undefined
Explanation: Declared variable without assignment has type 'undefined'.

13. Output of given code.


[Link](5 + 5 + '5');
Output:
105
Explanation: (5+5)=10 first, then concatenated with string '5' → '105'.

14. Output of ternary operation.


const value = 10;
[Link](value > 5 ? 'Yes' : 'No');
Output:
Yes
Explanation: Condition is true, hence 'Yes'.

15. Output of given code.


[Link](5 == '5');
Output:
true
Explanation: '==' performs type coercion, so string '5' is converted to number 5.

Common questions

Powered by AI

JavaScript variable scoping is determined by the 'var', 'let', and 'const' keywords. 'var' is function-scoped, meaning it is accessible within the entire function where it is declared. In contrast, 'let' and 'const' are block-scoped, limited to the block in which they are defined. This affects how variables must be managed and manipulated within functions and blocks. For developers, understanding these scoping rules is crucial for controlling variable access and preventing issues related to variable hoisting and shadowing, which can lead to unintended behavior and bugs in the code .

Event-driven programming in JavaScript is fundamental for creating interactive web applications. It allows programs to respond to various user events such as clicks, key strokes, and input changes in real-time. This architecture makes applications more dynamic and responsive, improving user experience. JavaScript achieves this through event listeners and handlers that are associated with specific events, enabling efficient interaction with the DOM and the overall interface .

JavaScript's support for asynchronous programming is crucial for developing interactive web applications where tasks like network requests or file operations may take time to complete. By supporting asynchronous execution, the language enables non-blocking operations, allowing the program to remain responsive while waiting for long-running operations to finish. JavaScript provides constructs such as callbacks, promises, and the async/await syntax to handle such scenarios effectively, enabling smoother, more efficient code .

'NaN', short for 'Not-a-Number', is a special value in JavaScript used to represent the result of an invalid or undefined numerical operation. Examples include parseInt('abc'), which results in NaN, or the expression 0/0. Moreover, despite its name, the type of NaN in JavaScript is actually 'number', highlighting its use as a numeric error indicator .

The 'const' keyword in JavaScript is used to declare variables that cannot be reassigned after their initial assignment, making them immutable references. This is different from 'var' and 'let', which allow reassignment. Like 'let', 'const' is block-scoped, meaning it is only accessible within the block in which it is declared. This feature helps in maintaining constant values throughout the program's execution, ensuring data integrity .

The 'var' keyword declares variables that are function-scoped and are hoisted to the top of their scope, allowing the variable to be accessed before its declaration. In contrast, 'let' variables are block-scoped, meaning they are only accessible within the block they are defined in. This prevents access outside that block and avoids accidental redeclaration within the same scope. Additionally, 'let' does not hoist the variable in the same manner as 'var', preventing access before its declaration within the block .

Implicit type coercion in JavaScript occurs when the language automatically converts values from one data type to another during operations involving different types. For example, in the expression '5' + 2, the number 2 is coerced into a string resulting in '52'. Conversely, '5' - 2 results in the number 3 since the string '5' is coerced into a number . Another example is true + 1, where the boolean value true is coerced into the number 1, resulting in 2 .

The '==' operator is known as the equality operator in JavaScript. It performs type coercion, meaning it converts operands to a common type before making the comparison, which can lead to unexpected results. For example, 5 == '5' evaluates to true. On the other hand, the '===' operator, or strict equality operator, checks both value and type without conversion, so 5 === '5' evaluates to false. This makes '===' a more reliable choice when strict equality comparisons are required .

JavaScript facilitates object inheritance primarily through its prototype model. Every object in JavaScript has an internal link to another object called its prototype. Through this chain, objects can inherit properties and methods from their prototype. This is orchestrated using object constructors, the prototype property, and the Object.create method, which allows the creation of a new object with a specified prototype. This prototype-based inheritance is a foundational element of the language's object-oriented capabilities .

The code 'console.log(5 + 5 + '5');' results in the output '105'. The operation begins with the arithmetic addition of 5 + 5, which equals 10. This result is then concatenated with the string '5' due to implicit type coercion, resulting in the final string '105' .

You might also like