Font size
WorksheetsJavaScript Day-2
Total questions: 25
Worksheet time: 16mins
What is a Map in JavaScript?
A collection of unique values
A collection of key-value pairs
A type of array
A function
Which method adds a key-value pair to a Map?
push()
add()
set()
append()
Which method retrieves a value by key from a Map?
get()
find()
lookup()
value()
Which method removes a key-value pair by key from a Map?
delete()
remove()
clear()
pop()
Which method checks if a key exists in a Map?
has()
contains()
exists()
check()
Which method iterates over Map entries?
forEach()
map()
filter()
filter()
A Map stores data in (a) pairs
What is the purpose of typeof in JavaScript?
To assign values
To check the data type of a variable
To convert data types
To declare variables
What is the output of typeof 42?
number
string
integer
numeric
Which method extracts only the integer part from a string like "10px"?
Number()
parseInt()
parseFloat()
Integer()
const map = new Map();
map.set('name', 'Alice');
map.set('age', 30);
for (let [key, value] of map) {
key === 'name'
? console.log(key)
: console.log(value);
}
age
30
name Alice
age 30
name Alice
name
30
typeof null returns (a) .
What is the primary purpose of typeof?
Convert a variable
Check the type of a variable
Declare a variable
Assign a variable
function add(val){
return val+val+5
}
function processInput(input) {
if (typeof input === "string") {
return input.toUpperCase();
} else if (typeof input === "number") {
return add(input);
}
return input;
}
console.log(processInput(6));
console.log(processInput("4"));
console.log(processInput(3))
console.log(processInput(parseInt("5"))) what output return ?
(a)
What is the output of console.log("5" - 2)
(a)
typeof {a:1} returns __________.
object
map
array
dict
let myMap = new Map();
let myObj = {fname: "John", lname: "Doe"};
myMap.set(myObj, "player");
for (let [key, value] of myMap) {
console.log(value)
} what is the Output in console ?
(a)
Which statement about Map insertion order is true?
It is random.
It is sorted alphabetically by keys.
maintains the exact order in which pairs were added
Numeric keys always come first.
typeof is useful in __________ statements.
loop
conditional
function declaration
object creation
What is type conversion in JavaScript?
Changing the variable name
Changing a value’s data type
Declaring a variable
Creating a function
_____ Type Conversion : Manually performed by the developer.
____ Type Conversion : Automatically performed by JavaScript.
(a)
const map = new Map();
map.set('name', 'Alice');
map.set('age', 30);
map.set('city', 'chennai');
for (let [key, value] of Array.from(map)) {
key === 'name' ? map.delete(key) : console.log(value);
key === 'city' ? map.set('name', 'Alice') : map.set('age', 20);
}
console.log( Array.from(map));
30
chennai
[ [ 'age', 30], [ 'city', 'chennai' ], [ 'name', 'Alice' ] ]
Alice
20
chennai
[ [ 'age', 30], [ 'city', 'chennai' ], [ 'name', 'Alice' ] ]
30
chennai
[ [ 'age', 20 ], [ 'city', 'chennai' ], [ 'name', 'Alice' ] ]
20
chennai
[ [ 'age', 20 ], [ 'city', 'chennai' ], [ 'name', 'Alice' ] ]
In a Map, if you use an object as a key and then change a property inside that object, can you still retrieve the value?
No, the key is lost.
Yes, because the object reference remains the same.
Only if the object is frozen.
Maps only support string keys.
const arr = [3, 6, 12, 15];
function add(a, b = 1) {
if (typeof a === "number" && typeof b === "number") {
return a + b;
}
}
function decrement(a, b = 1) {
if (typeof a === "number" && typeof b === "number") {
return a - b;
}
}
for (let i = 0; i < arr.length; i++) {
let addedData = add(arr[i], i+2);
let decrementedData = decrement(arr[i], i+3);
console.log(`Original: ${arr[i]} | Added: ${addedData} | Decremented: ${decrementedData}`);
}
Original: 3 | Added: 12 | Decremented: 0
Original: 6 | Added: 9 | Decremented: 8
Original: 12 | Added: 16 | Decremented: 7
Original: 15 | Added: 18 | Decremented: 9
Original: 3 | Added: 5 | Decremented: 0
Original: 6 | Added: 9 | Decremented: 2
Original: 12 | Added: 12 | Decremented: 7
Original: 15 | Added: 10 | Decremented: 3
Original: 3 | Added: 5 | Decremented: 0
Original: 6 | Added: 9 | Decremented: 2
Original: 12 | Added: 16 | Decremented: 7
Original: 15 | Added: 20 | Decremented: 9
Original: 3 | Added:2 | Decremented: 0
Original: 6 | Added: 9 | Decremented: 2
Original: 12 | Added: 16 | Decremented: 8
Original: 15 | Added: 20 | Decremented: 19
let str = "name";
console.log(Boolean(str));
console.log(!!str && str);
(a)
