Free sample · JS-Dev-101
JavaScript Developer practice questions: 10 free, with answers
10 questions from the JavaScript Developer Practice Exams package, all from the Variables, Types, and Collections domain. Choose your answer, then open it to see whether you were right and why each option is right or wrong. The real JS-Dev-101 exam has 60 questions in 105 minutes.
Reviewed by Nick Gorgotsias, Founder of ForceDigestEvery question is written by ForceDigest from the published exam outline. Nothing is copied from the real exam.
Question 1
What does this code log? ```js console.log(total); var total = 5; ```
- It throws a `TypeError`
- It throws a `ReferenceError`
- It logs `undefined`
- It logs `5`, the assigned value
Show the answer
Correct answer: C. It logs `undefined`
A `var` declaration is hoisted and initialized to `undefined`, so reading it before the assignment line gives `undefined`. The value 5 is assigned only when that line runs.
Question 2
Which statements about `var` are true? Choose 2.
- It must be initialized when declared
- It is function-scoped, not block-scoped
- It can't be read before its declaration
- It is block-scoped like `let`
- It can be redeclared in the same scope
Show the answer
Correct answers: B. It is function-scoped, not block-scoped · E. It can be redeclared in the same scope
`var` is scoped to the function (or the global scope), ignores blocks, and can be redeclared. It is hoisted as `undefined`, so it can be read early, and needs no initial value.
Question 3
What does this code log? ```js const a = { n: 1 }; const b = a; b.n = 2; console.log(a.n); ```
- It logs `1`
- It logs `undefined`
- It throws a `TypeError`
- It logs `2`
Show the answer
Correct answer: D. It logs `2`
Objects are shared by reference: `a` and `b` point to the same object, so changing `b.n` also changes `a.n`. Primitives, by contrast, are copied by value.
Question 4
What does this code log? ```js const price = 19.999; console.log(typeof price.toFixed(2), price.toFixed(2)); ```
- It logs `number 20.00`
- It logs `number 20`
- It logs `string 20.00`
- It logs `string 19.999`
Show the answer
Correct answer: C. It logs `string 20.00`
`toFixed(2)` rounds to two decimal places and returns a string, so 19.999 becomes "20.00". Convert it back with `Number()` if you need to calculate with it.
Question 5
Which `Date` method returns the day of the week?
- `getWeekday()`
- `getDay()`
- `getUTCDate()`
- `getDate()`
Show the answer
Correct answer: B. `getDay()`
`getDay()` returns the weekday, from 0 for Sunday to 6 for Saturday. `getDate()` returns the day of the month, and there is no `getWeekday()` method.
Question 6
Which comparisons are true? Choose 2.
- `null == 0`
- `[1] === [1]`
- `NaN == NaN`
- `null == undefined`
- `0 == ""`
Show the answer
Correct answers: D. `null == undefined` · E. `0 == ""`
`null` loosely equals only `undefined`, and `0 == ""` converts the empty string to 0. `null == 0` is false, NaN equals nothing, and two arrays are different objects.
Question 7
A quantity can be 0, which is valid. It should default to 1 only when it is `null` or `undefined`. Which expression fits?
- `!qty ? 1 : qty`
- `qty ?? 1`
- `qty || 1`
- `qty && 1`
Show the answer
Correct answer: B. `qty ?? 1`
Nullish coalescing, `??`, uses the right side only for null or undefined, so a quantity of 0 is kept. `||` and `!qty` treat 0 as missing and replace it with 1.
Question 8
Which call sorts an array of numbers called `prices` from lowest to highest?
- `prices.sort((a, b) => a - b)`
- `prices.sort((a, b) => b - a)`
- `prices.sort()` with no comparator
- `prices.sort((a, b) => a > b ? 1 : 0)`
Show the answer
Correct answer: A. `prices.sort((a, b) => a - b)`
A comparator returns a negative number to put `a` first, positive to put `b` first. `a - b` sorts ascending; `b - a` sorts descending, and a plain `sort()` compares strings.
Question 9
Code must return the first order whose total is over 100, as the order object itself. Which call fits?
- `orders.some((o) => o.total > 100)`
- `orders.filter((o) => o.total > 100)`
- `orders.findIndex((o) => o.total > 100)`
- `orders.find((o) => o.total > 100)`
Show the answer
Correct answer: D. `orders.find((o) => o.total > 100)`
`find` returns the first matching item, or undefined. `filter` returns an array of every match, `some` returns a boolean, and `findIndex` returns a position.
Question 10
What does this code log? ```js const user = { name: "Ada", greet() {}, nick: undefined, age: 36 }; console.log(JSON.stringify(user)); ```
- It logs `{name:"Ada",age:36}`
- It logs `{"name":"Ada","age":36}`
- It logs `{"name":"Ada","nick":null,"age":36}`
- It logs `{"name":"Ada","greet":{},"age":36}`
Show the answer
Correct answer: B. It logs `{"name":"Ada","age":36}`
`JSON.stringify` leaves out properties whose values are functions or undefined. JSON keys are always written in double quotes.
Want the rest?
The full package has 264 questions across every exam domain, timed exams at the real length, a study guide, flashcards and a readiness check. The Variables, Types, and Collections chapter and the readiness check are free to try.
Or Lifetime Access: $79 once for every certification on ForceDigest.