Questions and answers: JavaScript & TypeScript

Revise JavaScript & TypeScript: here is a selection of questions of every level, with the answer and an explanation. To test yourself without seeing the answers, play the quizzes.

Play the JavaScript & TypeScript quizzes

Easy

In what year did Brendan Eich create JavaScript at Netscape?

Answer : 1995

Brendan Eich designed JavaScript in 1995 at Netscape, in about ten days. The language has no kinship with Java, despite the name.

Which keyword declares a block-scoped variable whose value can be changed?

Answer : let

let declares a block-scoped variable that can be reassigned. const forbids reassignment, and var has function scope.

What does typeof return when applied to the string 'abc' in JavaScript?

Answer : string

The typeof operator returns a string describing the type of the value: for a string of characters, the result is string.

What does the expression 0 == '0' return in JavaScript?

Answer : true

The == operator compares after conversion: '0' becomes the number 0. With === the types must also be identical, and the result would be false.

Which property gives the number of elements of an array in JavaScript?

Answer : length

The length property gives the number of elements of an array: [1, 2, 3].length is 3.

Which method returns the first element that matches a CSS selector, such as 'div.carte'?

Answer : querySelector

querySelector accepts any CSS selector and returns the first element found, or null if none matches.

What is the role of addEventListener in the browser?

Answer : Register a function that is called when an event occurs

addEventListener attaches a function to an element for an event type, such as a click. The function is called each time the event occurs.

Which function turns a JavaScript object into text in JSON format?

Answer : JSON.stringify

JSON.stringify turns a JavaScript value into JSON text; JSON.parse does the opposite.

Medium

Which ECMAScript version introduced let, const, arrow functions and classes?

Answer : ES6 (ECMAScript 2015)

ES6, published in 2015, introduced let and const, arrow functions and classes.

What does console.log(x) print when it is placed before the line var x = 5 in the same script?

Answer : undefined

With var, the declaration is hoisted but not the assignment: x already exists, with the value undefined. With let, the same access throws a ReferenceError.

Inside an arrow function, what does this refer to?

Answer : That of the enclosing function, set where the function is written

An arrow function has no own this: it takes the this of the place where it is defined.

What is called a closure in JavaScript?

Answer : A function that keeps access to the variables of the scope where it was created

A closure keeps access to the variables of the enclosing function, even after that function has finished running.

Which JavaScript value indicates that a variable has been declared but has received no value?

Answer : undefined

undefined is the value of a variable declared without assignment. null represents an absence of value chosen explicitly by the programmer.

What does [1, 2, 3, 4].filter(n => n % 2 === 0) return?

Answer : [2, 4]

filter keeps the elements for which the function returns true: here the even numbers.

What does Promise.resolve(5).then(x => x * 2) return?

Answer : A promise that will be resolved with the value 10

The resolution value of then is the result of the callback function: here 5 multiplied by 2, that is 10.

What does a function marked async return in JavaScript?

Answer : Always a promise

An async function always returns a promise: the return value is wrapped, and an exception becomes a rejection.

Hard

In what year did Microsoft publish the first version of TypeScript?

Answer : 2012

TypeScript, led by Anders Hejlsberg, was published by Microsoft in October 2012: it is a typed superset of JavaScript.

What is the difference between any and unknown in TypeScript?

Answer : An unknown value must be checked before use, whereas any disables this check

any disables type checking on the value. unknown forces you to narrow the type before any use, which protects the code.

What does the type parameter T do in function identite<T>(x: T): T ?

Answer : It keeps the type of the received argument and returns it unchanged

T is a type variable: TypeScript infers it from the argument at each call, which keeps the precise type of the result.

In TypeScript, what does compatibility between two object types rely on?

Answer : Structure: what the object has matters, not the declared name

TypeScript is structural: an object is compatible if it has the expected properties with the right types, even without an explicit declaration.

Between two tasks, which queue does the event loop empty completely before moving on?

Answer : The microtask queue, such as promises

Promise callbacks form the microtask queue, which the loop empties completely between two tasks. Macrotasks, such as setTimeout, are processed one at a time.

When a property of a JavaScript object is missing, where does the engine look next?

Answer : In the prototype of the object, then along the prototype chain

The lookup climbs the prototype chain. If the property exists nowhere, the value read is undefined, without an error.

Which tsconfig.json option enables all the strict checks at once?

Answer : strict

strict: true enables notably strictNullChecks and noImplicitAny. Each of these options can also be set separately.

Which tool installs the dependencies listed in the package.json file of a Node.js project?

Answer : npm

npm reads the package.json file and installs the dependencies in the node_modules folder.

Related themes

Play the JavaScript & TypeScript quizzes