Javascript Questions
Javascript Questions
Toggle navigation
Sign in
Product
Actions
Automate any workflow
Packages
Host and manage packages
Security
Find and fix vulnerabilities
Codespaces
Instant dev environments
Copilot
Write better code with AI
Code review
Manage code changes
Issues
Plan and track work
Discussions
Collaborate outside of code
Explore
All features
Documentation
GitHub Skills
Blog
Solutions
For
Enterprise
Teams
Startups
Education
By Solution
CI/CD & Automation
DevOps
DevSecOps
Resources
Learning Pathways
White papers, Ebooks, Webinars
Customer Stories
Partners
Open Source
GitHub Sponsors
Fund open source developers
The ReadME Project
GitHub community articles
Repositories
Topics
Trending
Collections
Pricing
Search or jump to...
lydiahallie/javascript-questions
master Branches Tags Go to file Code
ar-AR ar-AR
ar-EG ar-EG
bs-BS bs-BS
de-DE de-DE
es-ES es-ES
fr-FR fr-FR
id-ID id-ID
it-IT it-IT
ja-JA ja-JA
ko-KR ko-KR
nl-NL nl-NL
pl-PL pl-PL
pt-BR pt-BR
ro-RO ro-RO
ru-RU ru-RU
sq-KS sq-KS
th-TH th-TH
tr-TR tr-TR
uk-UA uk-UA
vi-VI vi-VI
zh-CN zh-CN
zh-TW zh-TW
LICENSE LICENSE
README.md README.md
View all files
JavaScript Questions
Note
This repo was created in 2019 and the questions provided here are therefore based on the JavaScript
syntax and behaviour at that time. Since JavaScript is a constantly evolving language, there are newer
language features that are not covered by the questions here.
From basic to advanced: test how well you know JavaScript, refresh your knowledge a bit, or prepare for
your coding interview! 💪 🚀 I update this repo regularly with new questions. I added the answers in the
**collapsed sections** below the questions, simply click on them to expand it. It's just for fun, good luck! ❤️
Feel free to reach out to me! 😊
Instagram || Twitter || LinkedIn || Blog
Feel free to use them in a project! 😃 I would really appreciate a reference to this repo, I create
the questions and explanations (yes I'm sad lol) and the community helps me so much to
maintain and improve it! 💪🏼 Thank you and have fun!
See 20 Available Translations 🇸🇦🇪🇬🇧🇦🇩🇪🇪🇸🇫🇷🇮🇩🇯🇵🇰🇷🇳🇱🇧🇷🇷🇺🇹🇭🇹🇷🇺🇦🇻🇳🇨🇳🇹🇼
🇽🇰
function sayHi() {
console.log(name);
console.log(age);
var name = 'Lydia';
let age = 21;
}
sayHi();
A: 0 1 2 and 0 1 2
B: 0 1 2 and 3 3 3
C: 3 3 3 and 0 1 2
Answer
const shape = {
radius: 10,
diameter() {
return this.radius * 2;
},
perimeter: () => 2 * Math.PI * this.radius,
};
console.log(shape.diameter());
console.log(shape.perimeter());
A: 20 and 62.83185307179586
B: 20 and NaN
C: 20 and 63
D: NaN and 63
Answer
+true;
!'Lydia';
A: 1 and false
B: false and NaN
C: false and false
Answer
const bird = {
size: 'small',
};
const mouse = {
name: 'Mickey',
small: true,
};
A: Hello
B: Hey!
C: undefined
D: ReferenceError
E: TypeError
Answer
let a = 3;
let b = new Number(3);
let c = 3;
console.log(a == b);
console.log(a === b);
console.log(b === c);
class Chameleon {
static colorChange(newColor) {
this.newColor = newColor;
return this.newColor;
}
constructor({ newColor = 'green' } = {}) {
this.newColor = newColor;
}
}
const freddie = new Chameleon({ newColor: 'purple' });
console.log(freddie.colorChange('orange'));
A: orange
B: purple
C: green
D: TypeError
Answer
9. What's the output?
let greeting;
greetign = {}; // Typo!
console.log(greetign);
A: {}
B: ReferenceError: greetign is not defined
C: undefined
Answer
function bark() {
console.log('Woof!');
}
bark.animal = 'dog';
A: TypeError
B: SyntaxError
C: Lydia Hallie
D: undefined undefined
Answer
A: true
B: false
Answer
function sum(a, b) {
return a + b;
}
sum(1, '2');
A: NaN
B: TypeError
C: "12"
D: 3
Answer
let number = 0;
console.log(number++);
console.log(++number);
console.log(number);
A: 1 1 2
B: 1 2 2
C: 0 2 2
D: 0 1 2
Answer
function checkAge(data) {
if (data === { age: 18 }) {
console.log('You are an adult!');
} else if (data == { age: 18 }) {
console.log('You are still an adult.');
} else {
console.log(`Hmm.. You don't have an age I guess`);
}
}
checkAge({ age: 18 });
Answer
function getAge(...args) {
console.log(typeof args);
}
getAge(21);
A: "number"
B: "array"
C: "object"
D: "NaN"
Answer
function getAge() {
'use strict';
age = 21;
console.log(age);
}
getAge();
A: 21
B: undefined
C: ReferenceError
D: TypeError
Answer
A: 105
B: "105"
C: TypeError
D: "10*10+5"
Answer
sessionStorage.setItem('cool_secret', 123);
var num = 8;
var num = 10;
console.log(num);
A: 8
B: 10
C: SyntaxError
D: ReferenceError
Answer
A: { a: "one", b: "two" }
B: { b: "two", a: "three" }
C: { a: "three", b: "two" }
D: SyntaxError
Answer
26. The JavaScript global execution context creates two things for you: the global object, and the "this" keyword.
A: true
B: false
C: it depends
Answer
A: 1 2
B: 1 2 3
C: 1 2 4
D: 1 3 4
Answer
String.prototype.giveLydiaPizza = () => {
return 'Just give Lydia pizza already!';
};
const name = 'Lydia';
console.log(name.giveLydiaPizza())
const a = {};
const b = { key: 'b' };
const c = { key: 'c' };
a[b] = 123;
a[c] = 456;
console.log(a[b]);
A: 123
B: 456
C: undefined
D: ReferenceError
Answer
A: Outer div
B: Inner div
C: button
D: An array of all nested elements.
Answer
32. When you click the paragraph, what's the logged output?
<div onclick="console.log('div')">
<p onclick="console.log('p')">
Click here!
</p>
</div>
A: p div
B: div p
C: p
D: div
Answer
A: undefined is 21 Lydia is 21
B: function function
C: Lydia is 21 Lydia is 21
D: Lydia is 21 function
Answer
function sayHi() {
return (() => 0)();
}
console.log(typeof sayHi());
A: "object"
B: "number"
C: "function"
D: "undefined"
Answer
0;
new Number(0);
('');
(' ');
new Boolean(false);
undefined;
A: 0, '', undefined
B: 0, new Number(0), '', new Boolean(false), undefined
C: 0, '', new Boolean(false), undefined
D: All of them are falsy
Answer
(() => {
let x, y;
try {
throw new Error();
} catch (x) {
(x = 1), (y = 2);
console.log(x);
}
console.log(x);
console.log(y);
})();
A: 1 undefined 2
B: undefined undefined undefined
C: 1 1 2
D: 1 undefined undefined
Answer
A: primitive or object
B: function or object
C: trick question! only objects
D: number or object
Answer
!!null;
!!'';
!!1;
A: a unique id
B: the amount of milliseconds specified
C: the passed function
D: undefined
Answer
[...'Lydia'];
function* generator(i) {
yield i;
yield i * 2;
}
const gen = generator(10);
console.log(gen.next().value);
console.log(gen.next().value);
Answer
A: "one"
B: "two"
C: "two" "one"
D: "one" "two"
Answer
A: null
B: [null]
C: [{}]
D: [{ name: "Lydia" }]
Answer
const person = {
name: 'Lydia',
age: 21,
};
for (const item in person) {
console.log(item);
}
A: "345"
B: "75"
C: 12
D: "12"
Answer
A: 42
B: "42"
C: 7
D: NaN
Answer
A: []
B: [null, null, null]
C: [undefined, undefined, undefined]
D: [ 3 x empty ]
Answer
function Car() {
this.make = 'Lamborghini';
return { make: 'Maserati' };
}
const myCar = new Car();
console.log(myCar.make);
A: "Lamborghini"
B: "Maserati"
C: ReferenceError
D: TypeError
Answer
(() => {
let x = (y = 10);
})();
console.log(typeof x);
console.log(typeof y);
A: "undefined", "number"
B: "number", "number"
C: "object", "number"
D: "number", "undefined"
Answer
class Dog {
constructor(name) {
this.name = name;
}
}
Dog.prototype.bark = function() {
console.log(`Woof I am ${this.name}`);
};
const pet = new Dog('Mara');
pet.bark();
delete Dog.prototype.bark;
pet.bark();
A: [1, 1, 2, 3, 4]
B: [1, 2, 3, 4]
C: {1, 1, 2, 3, 4}
D: {1, 2, 3, 4}
Answer
// counter.js
let counter = 10;
export default counter;
// index.js
import myCounter from './counter';
myCounter += 1;
console.log(myCounter);
A: 10
B: 11
C: Error
D: NaN
Answer
A: false, true
B: "Lydia", 21
C: true, true
D: undefined, undefined
Answer
A: [[1, 2, 3, 4, 5]]
B: [1, 2, 3, 4, 5]
C: 1
D: [1]
Answer
const settings = {
username: 'lydiahallie',
level: 19,
health: 90,
};
const data = JSON.stringify(settings, ['level', 'health']);
console.log(data);
A: "{"level":19, "health":90}"
B: "{"username": "lydiahallie"}"
C: "["level", "health"]"
D: "{"username": "lydiahallie", "level":19, "health":90}"
Answer
A: 10, 10
B: 10, 11
C: 11, 11
D: 11, 12
Answer
A: 1 2 and 3 3 and 6 4
B: 1 2 and 2 3 and 3 4
C: 1 undefined and 2 undefined and 3 undefined and 4 undefined
D: 1 2 and undefined 3 and undefined 4
Answer
66. With which constructor can we successfully extend the Dog class?
class Dog {
constructor(name) {
this.name = name;
}
};
class Labrador extends Dog {
// 1
constructor(name, size) {
this.size = size;
}
// 2
constructor(name, size) {
super(name);
this.size = size;
}
// 3
constructor(size) {
super(name);
this.size = size;
}
// 4
constructor(name, size) {
this.name = name;
this.size = size;
}
};
A: 1
B: 2
C: 3
D: 4
Answer
// index.js
console.log('running index.js');
import { sum } from './sum.js';
console.log(sum(1, 2));
// sum.js
console.log('running sum.js');
export const sum = (a, b) => a + b;
Answer
console.log('🥑' + '💻');
A: "🥑💻"
B: 257548
C: A string containing their code points
D: Error
Answer
71. How can we log the values that are commented out after the console.log statement?
function* startGame() {
const answer = yield 'Do you love JavaScript?';
if (answer !== 'Yes') {
return "Oh wow... Guess we're done here";
}
return 'JavaScript loves you back ❤️';
}
const game = startGame();
console.log(/* 1 */); // Do you love JavaScript?
console.log(/* 2 */); // JavaScript loves you back ❤️
console.log(String.raw`Hello\nworld`);
A: Hello world!
B: Hello
world
C: Hello\nworld
D: Hello\n
world
Answer
A: ['apple', 'banana']
B: 2
C: true
D: undefined
Answer
A: { x: 100, y: 20 }
B: { x: 10, y: 20 }
C: { x: 100 }
D: ReferenceError
Answer
A: "Lydia"
B: "myName"
C: undefined
D: ReferenceError
Answer
function sum(a, b) {
return a + b;
}
A: Yes
B: No
Answer
Answer
function sayHi(name) {
return `Hi there, ${name}`;
}
console.log(sayHi());
A: Hi there,
B: Hi there, undefined
C: Hi there, null
D: ReferenceError
Answer
function checkAge(age) {
if (age < 18) {
const message = "Sorry, you're too young.";
} else {
const message = "Yay! You're old enough!";
}
return message;
}
console.log(checkAge(21));
fetch('https://www.website.com/api/user/1')
.then(res => res.json())
.then(res => console.log(res));
86. Which option is a way to set hasName equal to true, provided you cannot pass true as an argument?
function getName(name) {
const hasName = //
}
A: !!name
B: name
C: new Boolean(name)
D: name.length
Answer
A: """
B: "I"
C: SyntaxError
D: undefined
Answer
A: NaN
B: 20
C: ReferenceError
D: undefined
Answer
// module.js
export default () => 'Hello world';
export const name = 'Lydia';
// index.js
import * as data from './module';
console.log(data);
class Person {
constructor(name) {
this.name = name;
}
}
const member = new Person('John');
console.log(typeof member);
A: "class"
B: "function"
C: "object"
D: "string"
Answer
A: [1, 2, 3, 4, 5]
B: [1, 2, 3, 5]
C: [1, 2, 3, 4]
D: Error
Answer
function giveLydiaPizza() {
return 'Here is pizza!';
}
const giveLydiaChocolate = () =>
"Here's chocolate... now go hit the gym already.";
console.log(giveLydiaPizza.prototype);
console.log(giveLydiaChocolate.prototype);
const person = {
name: 'Lydia',
age: 21,
};
for (const [x, y] of Object.entries(person)) {
console.log(x, y);
}
function nums(a, b) {
if (a > b) console.log('a is bigger');
else console.log('b is bigger');
return
a + b;
}
console.log(nums(4, 2));
console.log(nums(1, 2));
class Person {
constructor() {
this.name = 'Lydia';
}
}
Person = class AnotherPerson {
constructor() {
this.name = 'Sarah';
}
};
const member = new Person();
console.log(member.name);
A: "Lydia"
B: "Sarah"
C: Error: cannot redeclare Person
D: SyntaxError
Answer
const info = {
[Symbol('a')]: 'b',
};
console.log(info);
console.log(Object.keys(info));
A: SyntaxError
B: ReferenceError
C: TypeError
D: undefined
Answer
A: false null []
B: null "" true
C: {} "" []
D: null null true
Answer
A: 3, NaN, NaN
B: 3, 7, NaN
C: 3, Lydia2, [object Object]2
D: "12", Lydia2, [object Object]2
Answer
Promise.resolve(5);
A: 5
B: Promise {<pending>: 5}
C: Promise {<fulfilled>: 5}
D: Error
Answer
const colorConfig = {
red: true,
blue: false,
green: true,
black: true,
yellow: false,
};
const colors = ['pink', 'red', 'blue'];
console.log(colorConfig.colors[1]);
A: true
B: false
C: undefined
D: TypeError
Answer
A: true
B: false
Answer
A: All of them
B: map reduce slice splice
C: map slice splice
D: splice
Answer
JSON.parse();
A: Lydia
B: Sarah
C: undefined
D: ReferenceError
Answer
function* generatorOne() {
yield ['a', 'b', 'c'];
}
function* generatorTwo() {
yield* ['a', 'b', 'c'];
}
const one = generatorOne();
const two = generatorTwo();
console.log(one.next().value);
console.log(two.next().value);
A: a and a
B: a and undefined
C: ['a', 'b', 'c'] and a
D: a and ['a', 'b', 'c']
Answer
A: I love to program
B: undefined to program
C: ${(x => x)('I love') to program
D: TypeError
Answer
let config = {
alert: setInterval(() => {
console.log('Alert!');
}, 1000),
};
config = null;
A: 1
B: 2
C: 2 and 3
D: All of them
Answer
const person = {
name: 'Lydia',
age: 21,
};
const changeAge = (x = { ...person }) => (x.age += 1);
const changeAgeAndName = (x = { ...person }) => {
x.age += 1;
x.name = 'Sarah';
};
changeAge(person);
changeAgeAndName();
console.log(person);
function sumValues(x, y, z) {
return x + y + z;
}
A: sumValues([...1, 2, 3])
B: sumValues([...[1, 2, 3]])
C: sumValues(...[1, 2, 3])
D: sumValues([1, 2, 3])
Answer
let num = 1;
const list = ['🥳', '🤠', '🥰', '🤪'];
console.log(list[(num += 1)]);
A: 🤠
B: 🥰
C: SyntaxError
D: ReferenceError
Answer
const person = {
firstName: 'Lydia',
lastName: 'Hallie',
pet: {
name: 'Mara',
breed: 'Dutch Tulip Hound',
},
getFullName() {
return `${this.firstName} ${this.lastName}`;
},
};
console.log(person.pet?.name);
console.log(person.pet?.family?.name);
console.log(person.getFullName?.());
console.log(member.getLastName?.());
const config = {
languages: [],
set language(lang) {
return this.languages.push(lang);
},
};
console.log(config.language);
A: false true
B: true false
C: false false
D: true true
Answer
A: 4 5 6
B: 6 5 4
C: 4 function function
D: undefined undefined 6
Answer
A: 1 2 3
B: {1: 1} {2: 2} {3: 3}
C: { 1: undefined } undefined undefined
D: undefined undefined undefined
Answer
A: "number"
B: "string"
C: undefined
D: ReferenceError
Answer
A: 0
B: 1
C: 2
D: 3
Answer
// sum.js
export default function sum(x) {
return x + x;
}
// index.js
import * as sum from './sum';
A: sum(4)
B: sum.sum(4)
C: sum.default(4)
D: Default aren't imported with *, only named exports
Answer
const handler = {
set: () => console.log('Added a new property!'),
get: () => console.log('Accessed a property!'),
};
const person = new Proxy({}, handler);
person.name = 'Lydia';
person.name;
Answer
const person = {
name: 'Lydia Hallie',
address: {
street: '100 Main St',
},
};
Object.freeze(person);
A: 2 4 and 3 6
B: 2 NaN and 3 NaN
C: 2 Error and 3 6
D: 2 4 and 3 Error
Answer
class Counter {
#number = 10
increment() {
this.#number++
}
getNum() {
return this.#number
}
}
const counter = new Counter()
counter.increment()
console.log(counter.#number)
A: 10
B: 11
C: undefined
D: SyntaxError
Answer
const teams = [
{ name: 'Team 1', members: ['Paul', 'Lisa'] },
{ name: 'Team 2', members: ['Laura', 'Tim'] },
];
function* getMembers(members) {
for (let i = 0; i < members.length; i++) {
yield members[i];
}
}
function* getTeams(teams) {
for (let i = 0; i < teams.length; i++) {
// ✨ SOMETHING IS MISSING HERE ✨
}
}
const obj = getTeams(teams);
obj.next(); // { value: "Paul", done: false }
obj.next(); // { value: "Lisa", done: false }
A: yield getMembers(teams[i].members)
B: yield* getMembers(teams[i].members)
C: return getMembers(teams[i].members)
D: return yield getMembers(teams[i].members)
Answer
const person = {
name: 'Lydia Hallie',
hobbies: ['coding'],
};
function addHobby(hobby, hobbies = person.hobbies) {
hobbies.push(hobby);
return hobbies;
}
addHobby('running', []);
addHobby('dancing');
addHobby('baking', person.hobbies);
console.log(person.hobbies);
A: ["coding"]
B: ["coding", "dancing"]
C: ["coding", "dancing", "baking"]
D: ["coding", "running", "dancing", "baking"]
Answer
class Bird {
constructor() {
console.log("I'm a bird. 🦢");
}
}
class Flamingo extends Bird {
constructor() {
console.log("I'm pink. 🌸");
super();
}
}
const pet = new Flamingo();
A: I'm pink. 🌸
B: I'm pink. 🌸 I'm a bird. 🦢
C: I'm a bird. 🦢 I'm pink. 🌸
D: Nothing, we didn't call any method
Answer
A: 1
B: 1 and 2
C: 3 and 4
D: 3
Answer
144. What do we need to add to the person object to get ["Lydia Hallie", 21] as the output of [...person]?
const person = {
name: "Lydia Hallie",
age: 21
}
[...person] // ["Lydia Hallie", 21]
let count = 0;
const nums = [0, 1, 2, 3];
nums.forEach(num => {
if (num) count += 1
})
console.log(count)
A: 1
B: 2
C: 3
D: 4
Answer
function getFruit(fruits) {
console.log(fruits?.[1]?.[1])
}
getFruit([['🍊', '🍌'], ['🍍']])
getFruit()
getFruit([['🍍'], ['🍊', '🍌']])
A: null, undefined, 🍌
B: [], null, 🍌
C: [], [], 🍌
D: undefined, undefined, 🍌
Answer
class Calc {
constructor() {
this.count = 0
}
increase() {
this.count ++
}
}
const calc = new Calc()
new Calc().increase()
console.log(calc.count)
A: 0
B: 1
C: undefined
D: ReferenceError
Answer
const user = {
email: "[email protected]",
password: "12345"
}
const updateUser = ({ email, password }) => {
if (email) {
Object.assign(user, { email })
}
if (password) {
user.password = password
}
return user
}
const updatedUser = updateUser({ email: "[email protected]" })
console.log(updatedUser === user)
A: false
B: true
C: TypeError
D: ReferenceError
Answer
const user = {
email: "[email protected]",
updateEmail: email => {
this.email = email
}
}
user.updateEmail("[email protected]")
console.log(user.email)
A: [email protected]
B: [email protected]
C: undefined
D: ReferenceError
Answer
153. What should the value of method be to log { name: "Lydia", age: 22 } ?
const keys = ["name", "age"]
const values = ["Lydia", 22]
const method = /* ?? */
Object[method](keys.map((_, i) => {
return [keys[i], values[i]]
})) // { name: "Lydia", age: 22 }
A: entries
B: values
C: fromEntries
D: forEach
Answer
Answer
About
A long list of (advanced) JavaScript questions, and their explanations ✨
Resources
Readme
License
MIT license
Activity
Stars
58.8k stars
Watchers
1.3k watching
Forks
8.4k forks
Report repository
Releases
No releases published
Packages
No packages published
Contributors 202
@lydiahallie
@jakeherp
@Tarabass
@tcoppin
@oshliaer
@sexyoung
@danah-kim
@SaraAli26
@mindsers
@jonastg
@qtomasicchio
@nedimf
@dotrungkien
@KL13NT
+ 188 contributors
Footer
© 2024 GitHub, Inc.
Footer navigation
Terms
Privacy
Security
Status
Docs
Contact
Manage cookies
Do not share my personal information