How well do you really know JavaScript?

It’s pretty well known that JavaScript has strange syntax and semantics. Let’s see how familiar you are with them! This quiz’s format is mostly inspired by this Java quiz.

Assume that code segments provided are executed in an isolated, modern ECMAScript environment.


1.

Consider the following code segment.

function get(array) {
	return array.at('start');
}

What is the result of calling the get function with the array ['first', 'second', 'third']?


2.

Consider the following code segment.

const a = '👋';
const b = '👋🏾';
console.log(a.length + b.length);

What value will be printed to the console when the code is executed?


3.

Which of the following is not always a reserved word?


4.

Consider the following code segment.

Array.prototype.push('hello');

console.log(Array.prototype.length);

What value will be printed to the console when the code is executed?


5.

Which of the following code segments are syntactically valid (i.e., does not throw a SyntaxError)?

  1. x++y
  2. x+++y
  3. x+++++y

6.

Consider the following code segment.

const history = [];

function tag(literals, ...values) {
	history.push(literals);
	return values;
}

function useTag() {
	return tag`Hello, ${'world'}!`;
}

const a = useTag() === useTag();
const b = history[0] === history[1];
const c = history[0].raw.length === 1;

Of the values a, b, and c, which are true?


7.

What is the result when the expression parseFloat('Infinity') === Number.POSITIVE_INFINITY is evaluated?


8.

Consider the following code segment.

function saferDivide(a, b) {
	try {
		return a / b;
	} finally {
		return 1;
	}
}

const x = saferDivide(1, 0);
const y = saferDivide(4, 2);

console.log(x + y);

What is the value printed to the console when the code is executed?


9.

Consider the following code segment.

const wrap = value => { value };
const array = [1, 2, 3].map(wrap);
const items = array.toString();

What is the value of items after the code is executed?


10.

Consider the following code segment.

let foo = { n: 1 };
let bar = foo;
foo.x = foo = { n: 2 };

What are the values of foo and bar after the code is executed? Assume that indicates that the objects are structurally equivalent and that self-references are circular.


11.

Consider the following code segment.

function f() {
	return f;
}
console.log(new f() instanceof f);

What is the value printed to the console when the code is executed?


12.

Which of the following code segments will run and print 'Hello world!' to the console without error?

  1. (new class {
    	constructor() {
    		console.log('Hello world!');
    		return Object.create(null);
    	}
    }());
  2. (new class {
    	class() {
    		console.log('Hello world!');
    	}
    }).class();
  3. (new class extends null {
    	constructor() {
    		console.log('Hello world!');
    		return Object.create(null);
    	}
    }());

13.

What is returned when the expression 'x'.padStart(4, Number) is evaluated?


14.

Consider the following code segment.

const buffer = new ArrayBuffer([1, 2, 3, 4]);
const array = new Float32Array(buffer);

for (let i = 0; i < array.length; i++) {
	array[i] = array[i] * array[i];
}

let sum = 0;

for (let i = 0; i < array.length; i++) {
	sum += array[i];
}

What is the value of sum after the code is executed?


15.

Consider the following code segment.

function set(value = 'hello') {
	'use strict';
	newValue = value;
}

set();

What is the result of calling the set function?


16.

Which of the following expressions is syntactically valid?

  1. false.true = 'maybe'
  2. 4.length = 5
  3. 'it'.is = /far/

17.

Consider the following code segment.

class Polygon {
	constructor() {
		this.name = 'Polygon';
	}
}

class Rectangle {
	constructor() {
		this.name = 'Rectangle';
	}
}

class Square extends Polygon {
	constructor() {
		super();
	}
}

Object.setPrototypeOf(Square, Rectangle);

const square = new Square();

console.log(square instanceof Polygon);
console.log(square instanceof Rectangle);
console.log(square.name);

What three values are printed to the console when the code is executed?


18.

Consider the following code segment.

class MyClass {
	static a = console.log('A');
	static {
		console.log('B');
	}
	static b = console.log('C');
	static {
		console.log('D');
	}
}

What is the order in which the letters A, B, C, and D are printed to the console when the code is executed?


19.

Consider the following code segment.

const { proxy, revoke } = Proxy.revocable(['Hello!'], {
	get: (target, prop) => prop.toString()
});

revoke();

console.log(`${proxy}`);

What is printed to the console when the code is executed?


20.

Consider the following code segment.

const obj = {};
obj['b'] = 1;
obj['1'] = 2;
obj['a'] = 3;
const keys = Object.keys(obj);

What is the value of keys after the code is executed?


21.

Consider the following code segment.

const numbers = new Uint8Array([11, 1, 2]);
const sorted = numbers.sort();

What is the value of sorted after the code is executed?


22.

What is the result of evaluating the expression `${import.meta}` in a JavaScript module?


23.

What are the values of a, b, and c after the following code is executed?

const a = Number();
const b = Number(undefined);
const c = Number(null);

24.

Which of the following import statements is syntactically valid, assuming that all referenced modules and exports exist?


25.

What is the result of evaluating the expression Math.toString()?


Results

You got 0 out of 0 questions! There are 25 questions unanswered. 🎉

This quiz was pretty fun to make. I tried to avoid a lot of the classic clichés like 0.1 + 0.2 === 0.3 and [] + [] === '', and went a bit deeper into some language features.

If you have any questions, suggestions, or feedback, you can reach out to me on Discord at ilotani or leave an issue on this site’s GitHub repository.