Question 1
function add(a, b) {
return a + b;
}
console.log(add(2, 3));
The function returns the sum of both arguments.
Question 2
const multiply = function(a, b) {
return a * b;
};
console.log(multiply(2, 4));
Function expressions can be stored in variables and invoked later.
Question 3
console.log(greet());
function greet() {
return "Hello";
}
Function declarations are fully hoisted.
Question 4
console.log(greet());
const greet = function() {
return "Hello";
};
The variable exists in the Temporal Dead Zone before initialization.
Question 5
function show(a, b) {
return a + b;
}
console.log(show(5));
Missing parameters become undefined. 5 + undefined produces NaN.
Question 6
function show(a = 10) {
return a;
}
console.log(show());
Default parameters are used when an argument is not supplied.
Question 7
const add = (a, b) => a + b; console.log(add(2, 5));
Arrow functions can return expressions directly.
Question 8
const square = x => x * x; console.log(square(4));
A single parameter does not require parentheses.
Question 9
const getUser = () => ({
name: "JS"
});
console.log(getUser().name);
Parentheses are required when returning an object literal implicitly.
Question 10
const getUser = () => {
name: "JS";
};
console.log(getUser());
Curly braces create a block body. No return statement exists.
Question 11
const double = x => x * 2; console.log(double(6));
The expression result is returned automatically.
Question 12
const add = (a, b, c) => a + b + c; console.log(add(1, 2, 3));
Multiple parameters require parentheses.
Question 13
const user = {
name: "JS",
getName() {
return this.name;
}
};
console.log(user.getName());
When called as an object method, this refers to the object.
Question 14
const user = {
name: "JS",
getName() {
return this.name;
}
};
const fn = user.getName;
console.log(fn());
The function is detached from the object. In modern JavaScript modules and strict mode, this becomes undefined.
Question 15
const obj = {
value: 10,
show() {
return this.value;
}
};
console.log(obj.show());
The method is invoked through the object, so this refers to obj.
Question 16
const user = {
name: "JS",
show() {
function inner() {
return this?.name;
}
return inner();
}
};
console.log(user.show());
inner() is called as a regular function. It loses the object context.
Question 17
const user = {
name: "JS",
show() {
const inner = () => this.name;
return inner();
}
};
console.log(user.show());
Arrow functions capture this from the surrounding scope.
Question 18
const user = {
name: "React",
getName() {
return this.name;
}
};
const fn = user.getName;
console.log(fn.call(user));
call() explicitly sets this.
Question 19
const user = {
value: 100,
show() {
setTimeout(function() {
console.log(this?.value);
}, 0);
}
};
user.show();
The callback executes as a normal function and loses the object context.
Question 20
const user = {
value: 100,
show() {
setTimeout(() => {
console.log(this.value);
}, 0);
}
};
user.show();
Arrow functions inherit this from show().
Question 21
function greet() {
return this.name;
}
const user = {
name: "JS"
};
console.log(
greet.call(user)
);
call() invokes the function immediately with the provided context.
Question 22
function add(a, b) {
return a + b;
}
console.log(
add.call(null, 5, 10)
);
Arguments after the first parameter are passed individually.
Question 23
const person = {
name: "JS"
};
const employee = {
name: "React"
};
function show() {
return this.name;
}
console.log(
show.call(employee)
);
call() allows method borrowing from another object.
Question 24
function multiply(a, b) {
return a * b;
}
console.log(
multiply.apply(null, [2, 4])
);
apply() accepts arguments as an array.
Question 25
function show(a, b) {
return a + b;
}
console.log(
show.apply(null, [3, 7])
);
apply() spreads values from the array into parameters.
Question 26
function greet() {
return this.name;
}
const user = {
name: "JavaScript"
};
const bound = greet.bind(user);
console.log(bound());
bind() creates a new function with fixed this.
Question 27
function add(a, b) {
return a + b;
}
const addFive =
add.bind(null, 5);
console.log(addFive(10));
bind() can partially apply arguments.
Question 28
function greet() {
return "Hello";
}
const fn = greet.bind(null);
console.log(fn());
bind() returns a new callable function.
Question 29
function show() {
return this.value;
}
const obj = {
value: 50
};
const bound =
show.bind(obj);
console.log(bound());
The bound function permanently uses obj as this.
Question 30
function greet() {
return this.name;
}
const user = {
name: "React"
};
const bound =
greet.bind(user);
console.log(bound.call({
name: "JS"
}));
Once a function is bound, call() cannot change its this value.
Question 31
function Person(name) {
this.name = name;
}
const user = new Person("JS");
console.log(user.name);
new creates a new object and binds this to it.
Question 32
function User() {
this.age = 25;
}
const user = new User();
console.log(user.age);
Properties assigned to this become instance properties.
Question 33
function Person(name) {
this.name = name;
}
const user = Person("JS");
console.log(user);
Without new, the function executes normally and returns undefined.
Question 34
function User(name) {
this.name = name;
}
User.prototype.getName =
function() {
return this.name;
};
const user = new User("React");
console.log(user.getName());
Prototype methods are available to all instances.
Question 35
const user = {
name: "JS",
getName: () => this?.name
};
console.log(user.getName());
Arrow functions do not get their own this.
Question 36
const user = {
name: "React",
getName() {
return () => this.name;
}
};
const fn = user.getName();
console.log(fn());
The arrow function captures this from getName().
Question 37
const user = {
name: "JS",
show: () => this?.name
};
console.log(user.show());
Arrow functions should not be used as object methods when this is needed.
Question 38
const arrow = () => this?.name;
console.log(
arrow.call({
name: "JS"
})
);
call() cannot change this for arrow functions.
Question 39
const arrow = () => this?.name;
const bound =
arrow.bind({
name: "React"
});
console.log(bound());
bind() cannot modify this inside arrow functions.
Question 40
const person = {
name: "JS"
};
function show() {
return this.name;
}
console.log(
show.call(person)
);
call() allows borrowing behavior from another object.
Question 41
const user = {
name: "React",
show() {
return this.name;
}
};
const admin = {
name: "Admin"
};
console.log(
user.show.call(admin)
);
call() changes this to admin.
Question 42
function greet() {
return this.name;
}
const user = {
name: "JS"
};
const a = greet.bind(user);
const b = a.bind({
name: "React"
});
console.log(b());
A bound function cannot be rebound.
Question 43
function User() {
this.name = "JS";
return {
name: "React"
};
}
const user = new User();
console.log(user.name);
Returning an object from a constructor replaces the created instance.
Question 44
function User() {}
User.prototype.role = "Admin";
const user = new User();
console.log(user.role);
JavaScript checks the prototype chain when a property is not found.
Question 45
const user = {
name: "JS",
show() {
return function() {
return this?.name;
};
}
};
const fn = user.show();
console.log(fn.call(user));
The returned regular function receives its this from call().