Scriptdrill

Level 2: Arrays and objects Exercise 16 of 45 Pro

Pick some keys

A JavaScript exercise from Scriptdrill. Solve it with a code editor in your browser that runs your function against example and hidden tests, and it is checked the moment you submit.

The task

Return a new object with only the listed `keys` of `obj`. Skip keys that `obj` does not have, but keep keys it has even when the value is `0`, `false`, `null`, `""` or `undefined`. Do not change `obj`.

What this exercise teaches

Object.hasOwn asks whether the key is there at all, so falsy values and even an explicit undefined are kept, while keys that were never set are skipped.

A hint if you get stuck

Test whether the key exists with `Object.hasOwn(obj, key)` (or `key in obj`), not by looking at the value.

This exercise is part of Pro

Pro unlocks all 45 exercises, with worked solutions and progress saved to your account. The free exercises work without an account.

You create a free account first, so Pro is attached to it.

Monthly is charged today and then every month until you cancel, in one click from your account page. Lifetime is one payment with no renewals. Full refund within 14 days of paying.

Start with the free exercises

More exercises in Arrays and objects