#JavaScript Questions
- What value is returned from the following statement?
function f() { return this }
f.bind(6).bind(9)()- What will the code below output to the console?
(function() {
var a = b = 3;
})();
console.log(typeof a, typeof b);- What will the code below output to the console?
var myObject = {
foo: 'bar',
func: function() {
var self = this;
(function() {
console.log(this.foo, self.foo);
}());
}
};
myObject.func();- What will the code below output to the console?
function foo1() {
return {
bar: 'hello'
};
}
function foo2() {
return
{
bar: 'hello'
};
}
console.log(foo1(), foo2());- What will the code below output to the console?
console.log(typeof NaN === 'number');
console.log(NaN === NaN);
console.log('abc' / 3);
console.log('A' - 'B' + '2');- In what order will the numbers 1-4 be logged to the console when the code below is executed?
(function() {
console.log(1);
setTimeout(function() {console.log(2)}, 1000);
setTimeout(function() {console.log(3)}, 0);
console.log(4);
}());- What will the code below output to the console?
var arr1 = 'john'.split('');
var arr2 = arr1.reverse();
var arr3 = 'jones'.split('');
arr2.push(arr3);
console.log(arr1.slice(-1), arr2.slice(-1));- What will the code below output to the console?
console.log(1 + '2' + '2');
console.log(1 + +'2' + '2');
console.log(1 + -'1' + '2');
console.log(+'1' + '1' + '2');- What will be the output of the following code?
for(var i = 0; i < 5; i++) {
setTimeout(function() { console.log(i); }, i * 1000);
}- What will the following code output to the console?
var spy = {
_name: 'James Bond',
getSecretName: function() {
return this._name;
}
};
var stoleSecretName = spy.getSecretName;
console.log(stoleSecretName(), spy.getSecretName());- What will the following code output to the console?
var a = {},
b = {key: 'b'},
c = {key: 'c'};
a[b] = 7;
a[c] = 13;
console.log(a[b]);- What would the following lines of code output to the console?
console.log(0 || 1);
console.log(1 || 2);
console.log(0 && 1);
console.log(1 && 2);- What will be the output of the following code?
var b = {a: 13};
(function foo(b) {
b = {a: 7};
})(b);
console.log(b);- What will be the output of the following code?
var a = 7;
var b = {a: 7};
(function foo(a, b) {
a = 13;
b.a = 13;
})(a, b);
console.log(a, b);- What will the following code output to the console?
function test() {
console.log(a, foo());
var a = 7;
function foo() {
return 13;
}
}
test();- What will the following code output to the console?
console.log(typeof {});
console.log(typeof []);
console.log(typeof null);
console.log(typeof undefined);- What will the following code output to the console?
function Person(name) {
this.name = name;
}
Person.prototype.printName = () => {
console.log(this.name);
}
let person = new Person('Mike');
person.printName();- What will the following code output to the console?
var person = {
age: 27,
printAge: function() {
console.log(this.age);
}
}
setTimeout(person.printAge, 1000);- What will the following code output to the console?
var arr = [77, 13, 33, 15, 7, 3];
arr.sort();
console.log(arr);- Consider the following code. What will be printed on the console if a user clicks the first and the fourth button in the list?
var nodes = document.getElementsByTagName('button');
for(var i = 0; i < nodes.length; i++) {
nodes[i].addEventListener('click', function() {
console.log('You clicked element #' + i);
});
}- How many data types are there in JS-ES5?
- What will the following code output to the console?
const person = {
age: 27
}
function editAge(age) {
person = {
age: age
};
}
editAge(30);
console.log(person.age);- What will the following code output to the console?
console.log(typeof foo);
console.log(typeof bar);
var foo = 7;
let bar = 13;- Consider the following code. What will be printed on the console?
const funcs = [];
for (var i = 0; i < 2; i++) {
funcs.push(() => console.log(i));
}
const start = i;
for (let i = start; i < 6; i++) {
funcs.push(() => console.log(i));
}
funcs.forEach((func) => func());- What will the following code output to the console?
let RegExp = '7';
console.log(typeof window.RegExp);
var Date = '13';
console.log(typeof window.Date);
const Array = '77';
console.log(typeof window.Array);- Consider the following code. What will be printed on the console?
function mixArgs(first, second = "b") {
console.log(first === arguments[0]);
first = "c";
console.log(first === arguments[0]);
}
mixArgs("a");
function args(first) {
console.log(first === arguments[0]);
first = "c";
console.log(first === arguments[0]);
}
args("a");- What will the following code output to the console?
function add(first, second = first) {
return first + second;
}
console.log(add(1));
function multiply(first = second, second) {
return first * second;
}
console.log(multiply(undefined, 1));- What will the following code output to the console?
let foo = (a, ...rest) => {
console.log(a, rest.length);
}
foo(7, 13, 33, 77);
let bar = (a, ...rest, b) => {
console.log(a, rest.length, b);
}
bar(7, 13, 33, 77);- What will the following code output to the console?
const person = {
name: 'Mike',
age: 27,
printName: function() {
console.log(this.name);
},
printAge: () => {
console.log(this.age);
}
};
person.printName();
person.printAge();-
Which statement(s) is/are true?
- Pure functions have side effects.
- Pure functions are usual functions.
- Pure functions will always produce the same output given the same inputs.
- Pure functions have no side effects.
- Pure functions will never produce the same output given the same inputs.
-
Consider the following code. Which function(s) is/are higher-order?
function add(x, y) {
return x + y;
}
function bind(func, context) {
return function() {
func.apply(context, arguments);
}
}
setTimeout(function() {console.log('Hello there!');}, 1000);-
Which statement(s) is/are true?
- Higher-order functions are usual functions.
- Higher-order functions either take functions as parameters, return functions or both.
- Higher-order functions either take objects as parameters, return objects or both.
- Higher-order function is just an another term of usual function.
-
Which statement is true?
- A curried function is a function that takes any number of parameters.
- A curried function is a function that only takes a single parameter at a time.
- A curried function is a function that does not take any parameters.
- A curried function is a function that only takes two parameters at a time.
-
Consider the following code. Which function(s) is/are pure?
var x = 7;
function add(y) {
return x + y;
}
var z = 3;
function multiply(x, y) {
return x * y;
}
function divide() {
x = x / 2;
}- Consider the following code. Which function(s) is/are pure?
var x = 7;
function add(y) {
return x + y;
}
var z = 3;
function multiply(x, y) {
return x * y;
}
function divide() {
x = x / 2;
}- Consider the following code. Which function(s) is/are optimized?
function factorial_1(n, p = 1) {
if (n <= 1) {
return 1 * p;
} else {
return factorial_1(n - 1, n * p);
}
}
function factorial_2(n) {
if(n <= 1) {
return 1;
} else {
return n * factorial_2(n - 1);
}
}- Consider the following code. Which function(s) is/are curried?
const multiply = (x) => x * 2;
const add = (x) => (y) => x + y;
const divide = (x) => (y) => y * y;- What will the following code output to the console?
const person = {
name: 'Mike',
age: 30
};
let name = 'Oleg';
let age = 27;
{name, age} = person;
console.log(name, age);- What will the code below output to the console?
function printPerson({name = 'Mike', age = 27}) {
console.log(name, age);
}
printPerson();- What will the following code return?
(function(x, f = () => x) {
var x;
var y = x;
x = 2;
return [x, y, f()];
})(1)- What will the following code output to the console?
const person = (name) => {name: name}
const p = person('Mike');
console.log(p.name);- What will the following code output to the console?
let arr = [];
for (let { x = 2, y } of [{ x: 1 }, 2, { y }]) {
arr.push(x, y);
}
console.log(arr);- What will the following code output to the console?
console.log(typeof `${{Object}}`.prototype);- What will the following code return?
(function() {
let f = this ? class g { } : class h { };
return [
typeof f,
typeof h
];
})();- What will the following code return?
(function() {
if(false) {
let f = { g() => 1 };
}
return typeof f;
})()- What will the following code return?
(function() {
return [
(() => this.x).bind({ x: 'inner' })(),
(() => this.x)()
]
}).call({ x: 'outer' });- What will the following code output to the console?
var output = (function(x) {
delete x;
return x;
})(0);
console.log(output);- What will the following code output to the console?
var Person = {
name: 'Jack'
}
var person = Object.create(Person);
delete person.name;
console.log(person.name);- What will the following code output to the console?
var foo = function bar() { return 12; };
console.log(typeof bar()); - What will the following code output to the console?
var salary = "1000$";
(function () {
console.log(salary);
var salary = "5000$";
console.log(salary);
})();-
Which statement(s) is/are true?
- The closure has access to variables declared in their own scope.
- The closure has access to variables declared in a parent function scope.
- The closure has access to variables declared in the global namespace.
-
What will the following code output to the console?
var y = 1;
if(function f() { }) {
y += typeof f;
}
console.log(y);- What will the following code output to the console?
console.log(0.1 + 0.2 == 0.3);- What will the following code output to the console?
var state = {
arr: [1, 2, 3]
};
function isEqual(arr) {
console.log(arr === state.arr);
}
var newArr = state.arr;
newArr.push(4);
isEqual(newArr);- What will the following code output to the console?
var state = {
arr: [1, 2, 3]
};
function isEqual(arr) {
console.log(arr === state.arr);
}
var newArr = state.arr.concat(4);
isEqual(newArr);- What will the following code output to the console?
const arr = Array(1, 2, 3)
arr.concat = () => 'Error'
delete arr.concat
console.log(arr.concat(5))- What will the following code output to the console?
Array.__proto__.concat = () => 'Error';
const arr1 = Array(1, 2, 3);
console.log(arr1.concat(5));
Array.prototype.concat = () => 'Error';
const arr2 = Array(1, 2, 3);
console.log(arr2.concat(5));- What will the following code output to the console?
const partial = (f, ...args) =>
(...moreArgs) => f(...args, ...moreArgs);
const add3 = (a, b, c) => a + b + c;
const plus = partial(add3, 2)
console.log(plus(2));- What will the following code output to the console?
const liftA2 = (f) =>
(a, b) => a.map(f).map((func) => func(b));
const func = a => b => a * b;
const liftedMult = liftA2(func);
console.log(liftedMult([1, 2], 3));- What will the following code output?
var salary = "1000$";
(function () {
console.log("Original salary was " + salary);
var salary = "5000$";
console.log("My New Salary " + salary);
})();- What will the following code output?
const x = {
val: 2
};
const x1 = () => x.val += 1;
const x2 = () => x.val *= 2;
x1();
x2();
console.log(x.val);- What will the following code output?
const output = ((x) => {
delete x;
return x;
})(7);
console.log(output);- What will the following code output?
var f = function g(){ return 23; };
typeof g();- What will the following code output?
(function(foo) {
return typeof foo.bar;
})({ foo: { bar: 1 } });- What will the following code output?
var foo = {
bar: function(){ return this.baz; },
baz: 1
}
typeof (f = foo.bar)();- What will the following code output?
[...[...'...']].length- What will the following code output?
(function f(f) {
return typeof f();
})(function(){ return 1; });- What will the following code output?
(function(x) {
return (function(y) {
console.log(x);
})(2)
})(1);- What will the following code output?
var foo = function bar(){ return 12; };
typeof bar();- What will the following code output?
x = 1;
console.log('x = ' + x);
var x;- What will the following code output?
let c = 4;
if (true) {
var c = 5;
}
console.log(c)- What will the following code output?
let arr = [1, 2, 3];
arr.a = 5;
console.log(arr.a);