|
2 | 2 |
|
3 | 3 | I post daily multiple choice JavaScript questions on my [Instagram](https://www.instagram.com/theavocoder), which I'll also post here!
|
4 | 4 |
|
5 |
| -From basic to advanced: test how well you know JavaScript, refresh your knowledge a bit, or prepare for your coding interview! :muscle: :rocket: I update this repo weekly with new questions. Last update: <a href=#20190805><b>August 5th</b></a> |
| 5 | +From basic to advanced: test how well you know JavaScript, refresh your knowledge a bit, or prepare for your coding interview! :muscle: :rocket: I update this repo weekly with new questions. Last update: <a href=#20190817><b>August 17th</b></a> |
6 | 6 |
|
7 | 7 | The answers are in the collapsed sections below the questions, simply click on them to expand it. Good luck :heart:
|
8 | 8 |
|
@@ -1575,17 +1575,17 @@ function sayHi() {
|
1575 | 1575 | const data = greeting();
|
1576 | 1576 | console.log("It worked!", data);
|
1577 | 1577 | } catch (e) {
|
1578 |
| - console.log("Oh no an error!", e); |
| 1578 | + console.log("Oh no an error:", e); |
1579 | 1579 | }
|
1580 | 1580 | }
|
1581 | 1581 |
|
1582 | 1582 | sayHi();
|
1583 | 1583 | ```
|
1584 | 1584 |
|
1585 |
| -- A: `"It worked! Hello world!"` |
1586 |
| -- B: `"Oh no an error: undefined` |
| 1585 | +- A: `It worked! Hello world!` |
| 1586 | +- B: `Oh no an error: undefined` |
1587 | 1587 | - C: `SyntaxError: can only throw Error objects`
|
1588 |
| -- D: `"Oh no an error: Hello world!` |
| 1588 | +- D: `Oh no an error: Hello world!` |
1589 | 1589 |
|
1590 | 1590 | <details><summary><b>Answer</b></summary>
|
1591 | 1591 | <p>
|
@@ -2982,3 +2982,243 @@ getItems(["banana", "apple"], "pear", "orange")
|
2982 | 2982 | The above example works. This returns the array `[ 'banana', 'apple', 'orange', 'pear' ]`
|
2983 | 2983 | </p>
|
2984 | 2984 | </details>
|
| 2985 | + |
| 2986 | +--- |
| 2987 | + |
| 2988 | +###### <a name=20190817></a>95. What's the output? |
| 2989 | + |
| 2990 | +```javascript |
| 2991 | +function nums(a, b) { |
| 2992 | + if |
| 2993 | + (a > b) |
| 2994 | + console.log('a is bigger') |
| 2995 | + else |
| 2996 | + console.log('b is bigger') |
| 2997 | + return |
| 2998 | + a + b |
| 2999 | +} |
| 3000 | + |
| 3001 | +console.log(nums(4, 2)) |
| 3002 | +console.log(nums(1, 2)) |
| 3003 | +``` |
| 3004 | + |
| 3005 | +- A: `a is bigger`, `6` and `b is bigger`, `3` |
| 3006 | +- B: `a is bigger`, `undefined` and `b is bigger`, `undefined` |
| 3007 | +- C: `undefined` and `undefined` |
| 3008 | +- D: `SyntaxError` |
| 3009 | + |
| 3010 | +<details><summary><b>Answer</b></summary> |
| 3011 | +<p> |
| 3012 | + |
| 3013 | +#### Answer: B |
| 3014 | + |
| 3015 | +In JavaScript, we don't _have_ to write the semicolon (`;`) explicitly, however the JavaScript engine still adds them after statements. This is called **Automatic Semicolon Insertion**. A statement can for example be variables, or keywords like `throw`, `return`, `break`, etc. |
| 3016 | + |
| 3017 | +Here, we wrote a `return` statement, and another value `a + b` on a _new line_. However, since it's a new line, the engine doesn't know that it's actually the value that we wanted to return. Instead, it automatically added a semicolon after `return`. You could see this as: |
| 3018 | + |
| 3019 | +```javascript |
| 3020 | + return; |
| 3021 | + a + b |
| 3022 | +``` |
| 3023 | + |
| 3024 | +This means that `a + b` is never reached, since a function stops running after the `return` keyword. If no value gets returned, like here, the function returns `undefined`. Note that there is no automatic insertion after `if/else` statements! |
| 3025 | + |
| 3026 | +</p> |
| 3027 | +</details> |
| 3028 | + |
| 3029 | +--- |
| 3030 | + |
| 3031 | +###### 96. What's the output? |
| 3032 | + |
| 3033 | +```javascript |
| 3034 | +class Person { |
| 3035 | + constructor() { |
| 3036 | + this.name = "Lydia" |
| 3037 | + } |
| 3038 | +} |
| 3039 | + |
| 3040 | +Person = class AnotherPerson { |
| 3041 | + constructor() { |
| 3042 | + this.name = "Sarah" |
| 3043 | + } |
| 3044 | +} |
| 3045 | + |
| 3046 | +const member = new Person() |
| 3047 | +console.log(member.name) |
| 3048 | +``` |
| 3049 | + |
| 3050 | +- A: `"Lydia"` |
| 3051 | +- B: `"Sarah"` |
| 3052 | +- C: `Error: cannot redeclare Person` |
| 3053 | +- D: `SyntaxError` |
| 3054 | + |
| 3055 | +<details><summary><b>Answer</b></summary> |
| 3056 | +<p> |
| 3057 | + |
| 3058 | +#### Answer: B |
| 3059 | + |
| 3060 | +We can set classes equal to other classes/function constructors. In this case, we set `Person` equal to `AnotherPerson`. The name on this constructor is `Sarah`, so the name property on the new `Person` instance `member` is `"Sarah"`. |
| 3061 | + |
| 3062 | +</p> |
| 3063 | +</details> |
| 3064 | + |
| 3065 | +--- |
| 3066 | + |
| 3067 | +###### 97. What's the output? |
| 3068 | + |
| 3069 | +```javascript |
| 3070 | +const info = { |
| 3071 | + [Symbol('a')]: 'b' |
| 3072 | +} |
| 3073 | + |
| 3074 | +console.log(info) |
| 3075 | +console.log(Object.keys(info)) |
| 3076 | +``` |
| 3077 | + |
| 3078 | +- A: `{Symbol('a'): 'b'}` and `["{Symbol('a')"]` |
| 3079 | +- B: `{}` and `[]` |
| 3080 | +- C: `{ a: "b" }` and `["a"]` |
| 3081 | +- D: `{Symbol('a'): 'b'}` and `[]` |
| 3082 | + |
| 3083 | +<details><summary><b>Answer</b></summary> |
| 3084 | +<p> |
| 3085 | + |
| 3086 | +#### Answer: D |
| 3087 | + |
| 3088 | +A Symbol is not _enumerable_. The Object.keys method returns all _enumerable_ key properties on an object. The Symbol won't be visible, and an empty array is returned. When logging the entire object, all properties will be visible, even non-enumerable ones. |
| 3089 | + |
| 3090 | +This is one of the many qualities of a symbol: besides representing an entirely unique value (which prevents accidental name collision on objects, for example when working with 2 libraries that want to add properties to the same object), you can also "hide" properties on objects this way (although not entirely. You can still access symbols using the `Object.getOwnPropertySymbols()` method). |
| 3091 | + |
| 3092 | +</p> |
| 3093 | +</details> |
| 3094 | + |
| 3095 | +--- |
| 3096 | + |
| 3097 | +###### 98. What's the output? |
| 3098 | + |
| 3099 | +```javascript |
| 3100 | +const getList = ([x, ...y]) => [x, y] |
| 3101 | +const getUser = user => { name: user.name, age: user.age } |
| 3102 | + |
| 3103 | +const list = [1, 2, 3, 4] |
| 3104 | +const user = { name: "Lydia", age: 21 } |
| 3105 | + |
| 3106 | +console.log(getList(list)) |
| 3107 | +console.log(getUser(user)) |
| 3108 | +``` |
| 3109 | + |
| 3110 | +- A: `[1, [2, 3, 4]]` and `undefined` |
| 3111 | +- B: `[1, [2, 3, 4]]` and `{ name: "Lydia", age: 21 }` |
| 3112 | +- C: `[1, 2, 3, 4]` and `{ name: "Lydia", age: 21 }` |
| 3113 | +- D: `Error` and `{ name: "Lydia", age: 21 }` |
| 3114 | + |
| 3115 | +<details><summary><b>Answer</b></summary> |
| 3116 | +<p> |
| 3117 | + |
| 3118 | +#### Answer: A |
| 3119 | + |
| 3120 | +The `getList` function receives an array as its argument. Between the parentheses of the `getList` function, we destructure this array right away. You could see this as: |
| 3121 | + |
| 3122 | + `[x, ...y] = [1, 2, 3, 4]` |
| 3123 | + |
| 3124 | + With the rest parameter `...y`, we put all "remaining" arguments in an array. The remaining arguments are `2`, `3` and `4` in this case. The value of `y` is an array, containig all the rest parameters. The value of `x` is equal to `1` in this case, so when we log `[x, y]`, `[1, [2, 3, 4]]` gets logged. |
| 3125 | + |
| 3126 | + The `getUser` function receives an object. With arrow functions, we don't _have_ to write curly brackets if we just return one value. However, if you want to return an _object_ from an arrow function, you have to write it between parentheses, otherwise no value gets returned! The following function would have returned an object: |
| 3127 | + |
| 3128 | +```const getUser = user => ({ name: user.name, age: user.age })``` |
| 3129 | + |
| 3130 | +Since no value gets returned in this case, the function returns `undefined`. |
| 3131 | + |
| 3132 | +</p> |
| 3133 | +</details> |
| 3134 | + |
| 3135 | +--- |
| 3136 | + |
| 3137 | +###### 99. What's the output? |
| 3138 | + |
| 3139 | +```javascript |
| 3140 | +const name = "Lydia" |
| 3141 | + |
| 3142 | +console.log(name()) |
| 3143 | +``` |
| 3144 | + |
| 3145 | +- A: `SyntaxError` |
| 3146 | +- B: `ReferenceError` |
| 3147 | +- C: `TypeError` |
| 3148 | +- D: `undefined` |
| 3149 | + |
| 3150 | +<details><summary><b>Answer</b></summary> |
| 3151 | +<p> |
| 3152 | + |
| 3153 | +#### Answer: C |
| 3154 | + |
| 3155 | +The variable `name` holds the value of a string, which is not a function, thus cannot invoke. |
| 3156 | + |
| 3157 | +TypeErrors get thrown wehn a value is not of the expected type. JavaScript expected `name` to be a function since we're trying to invoke it. It was a string however, so a TypeError gets thrown: name is not a function! |
| 3158 | + |
| 3159 | +SyntaxErrors get thrown when you've written something that isn't valid JavaScript, for example when you've written the word `return` as `retrun`. |
| 3160 | +ReferenceErrors get thrown when JavaScript isn't able to find a reference to a value that you're trying to access. |
| 3161 | + |
| 3162 | +</p> |
| 3163 | +</details> |
| 3164 | + |
| 3165 | +--- |
| 3166 | + |
| 3167 | +###### 100. What's the value of output? |
| 3168 | + |
| 3169 | +```javascript |
| 3170 | +// 🎉✨ This is my 100th question! ✨🎉 |
| 3171 | + |
| 3172 | +const output = `${[] && 'Im'}possible! |
| 3173 | +You should${'' && `n't`} see a therapist after so much JavaScript lol` |
| 3174 | +``` |
| 3175 | + |
| 3176 | +- A: `possible! You should see a therapist after so much JavaScript lol` |
| 3177 | +- B: `Impossible! You should see a therapist after so much JavaScript lol` |
| 3178 | +- C: `possible! You shouldn't see a therapist after so much JavaScript lol` |
| 3179 | +- D: `Impossible! You shouldn't see a therapist after so much JavaScript lol` |
| 3180 | + |
| 3181 | +<details><summary><b>Answer</b></summary> |
| 3182 | +<p> |
| 3183 | + |
| 3184 | +#### Answer: B |
| 3185 | + |
| 3186 | +`[]` is a truthy value. With the `&&` operator, the right-hand value will be returned if the left-hand value is a truthy value. In this case, the left-hand value `[]` is a truthy value, so `"Im'` gets returned. |
| 3187 | + |
| 3188 | +`""` is a falsy value. If the left-hand value is falsy, nothing gets returned. `n't` doesn't get returned. |
| 3189 | + |
| 3190 | +</p> |
| 3191 | +</details> |
| 3192 | + |
| 3193 | +--- |
| 3194 | + |
| 3195 | +###### 101. What's the value of output? |
| 3196 | + |
| 3197 | +```javascript |
| 3198 | +const one = (false || {} || null) |
| 3199 | +const two = (null || false || "") |
| 3200 | +const three = ([] || 0 || true) |
| 3201 | + |
| 3202 | +console.log(one, two, three) |
| 3203 | +``` |
| 3204 | + |
| 3205 | +- A: `false` `null` `[]` |
| 3206 | +- B: `null` `""` `true` |
| 3207 | +- C: `{}` `""` `[]` |
| 3208 | +- D: `null` `null` `true` |
| 3209 | + |
| 3210 | +<details><summary><b>Answer</b></summary> |
| 3211 | +<p> |
| 3212 | + |
| 3213 | +#### Answer: C |
| 3214 | + |
| 3215 | +With the `||` operator, we can return the first truthy operand. If all values are falsy, the last operand gets returned. |
| 3216 | + |
| 3217 | +`(false || {} || null)`: the empty object `{}` is a truthy value. This is the first (and only) truthy value, which gets returned. `one` is equal to `{}`. |
| 3218 | + |
| 3219 | +`(null || false || "")`: all operands are falsy values. This means that the past operand, `""` gets returned. `two` is equal to `""`. |
| 3220 | + |
| 3221 | +`([] || 0 || "")`: the empty array`[]` is a truthy value. This is the first truthy value, which gets returned. `three` is equal to `[]`. |
| 3222 | + |
| 3223 | +</p> |
| 3224 | +</details> |
0 commit comments