JavaScript Quiz Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q

What is the output?

function sayHi() {
console.log(name);
console.log(age);
var name = ‘Lydia’;
let age = 21;
}

sayHi();

A

undefined and ReferenceError

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is the output in the console?

+true;
!’Lydia’;

A

1 and false

The unary plus tries to convert an operand to a number. true is 1, and false is 0.

The string ‘Lydia’ is a truthy value. What we’re actually asking, is “is this truthy value falsy?”. This returns false.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

let c = { greeting: ‘Hey!’ };
let d;

d = c;
c.greeting = ‘Hello’;
console.log(d.greeting);

A

‘Hello’

In JavaScript, all objects interact by reference when setting them equal to each other.

First, variable c holds a value to an object. Later, we assign d with the same reference that c has to the object.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

let a = 3;
let b = new Number(3);
let c = 3;

console.log(a == b);
console.log(a === b);
console.log(b === c);

A

true false false

new Number() is a built-in function constructor. Although it looks like a number, it’s not really a number: it has a bunch of extra features and is an object.

When we use the == operator, it only checks whether it has the same value. They both have the value of 3, so it returns true.

However, when we use the === operator, both value and type should be the same. It’s not: new Number() is not a number, it’s an object. Both return false.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Is this valid?
function bark() {
console.log(‘Woof!’);
}

bark.animal = ‘dog’;

A

This is possible in JavaScript, because functions are objects! (Everything besides primitive types are objects)

A function is a special type of object. The code you write yourself isn’t the actual function. The function is an object with properties. This property is invocable.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

let number = 0;
console.log(number++);
console.log(++number);
console.log(number);

A

0 2 2

The postfix unary operator ++:

Returns the value (this returns 0)
Increments the value (number is now 1)
The prefix unary operator ++:

Increments the value (number is now 2)
Returns the value (this returns 2)
This returns 0 2 2.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

function getAge(…args) {
console.log(typeof args);
}

getAge(21);

A

The rest parameter (…args) lets us “collect” all remaining arguments into an array. An array is an object, so typeof args returns “object”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

const a = {};
const b = { key: ‘b’ };
const c = { key: ‘c’ };

a[b] = 123;
a[c] = 456;

console.log(a[b]);

A

456
Object keys are automatically converted into strings. We are trying to set an object as a key to object a, with the value of 123.

However, when we stringify an object, it becomes “[object Object]”. So what we are saying here, is that a[“[object Object]”] = 123. Then, we can try to do the same again. c is another object that we are implicitly stringifying. So then, a[“[object Object]”] = 456.

Then, we log a[b], which is actually a[“[object Object]”]. We just set that to 456, so it returns 456.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is the output?
!!null;
!!’’;
!!1;

A

false false true

null is falsy. !null returns true. !true returns false.

”” is falsy. !”” returns true. !true returns false.

1 is truthy. !1 returns false. !false returns true.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

const fruit = [‘🍌’, ‘🍊’, ‘🍎’]

fruit.slice(0, 1)
fruit.splice(0, 1)
fruit.unshift(‘🍇’)

console.log(fruit)

A

[‘🍇’, ‘🍊’, ‘🍎’]

First, we invoke the slice method on the fruit array. The slice method does not modify the original array, but returns the value that it sliced off the array: the banana emoji. Then, we invoke the splice method on the fruit array. The splice method does modify the original array, which means that the fruit array now consists of [‘🍊’, ‘🍎’]. At last, we invoke the unshift method on the fruit array, which modifies the original array by adding the provided value, ‘🍇’ in this case, as the first element in the array. The fruit array now consists of [‘🍇’, ‘🍊’, ‘🍎’].

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

let newList = [1, 2, 3].push(4);

console.log(newList.push(5));

A

Error
The .push method returns the new length of the array, not the array itself! By setting newList equal to [1, 2, 3].push(4), we set newList equal to the new length of the array: 4.

Then, we try to use the .push method on newList. Since newList is the numerical value 4, we cannot use the .push method: a TypeError is thrown.

92

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

const myFunc = ({ x, y, z }) => {
console.log(x, y, z);
};

myFunc(1, 2, 3);

A

undefined
undefined
undefined

myFunc expects an object with properties x, y and z as its argument. Since we’re only passing three separate numeric values (1, 2, 3) instead of one object with properties x, y and z ({x: 1, y: 2, z: 3}), x, y and z have their default value of undefined.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

const name = ‘Lydia’;
age = 21;

console.log(delete name);
console.log(delete age);

A

false true

The delete operator returns a boolean value: true on a successful deletion, else it’ll return false. However, variables declared with the var, const or let keyword cannot be deleted using the delete operator.

The name variable was declared with a const keyword, so its deletion is not successful: false is returned. When we set age equal to 21, we actually added a property called age to the global object. You can successfully delete properties from objects this way, also the global object, so delete age returns true.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

const list = [1 + 2, 1 * 2, 1 / 2];
console.log(list);

A

[3, 2, 0.5]

Array elements can hold any value. Numbers, strings, objects, other arrays, null, boolean values, undefined, and other expressions such as dates, functions, and calculations.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

console.log(‘I want pizza’[0]);

A

In order to get a character at a specific index of a string, you can use bracket notation. The first character in the string has index 0, and so on. In this case, we want to get the element with index 0, the character “I’, which gets logged.

Note that this method is not supported in IE7 and below. In that case, use .charAt().

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What are the falsey values in JavaScript? What does it mean when a value is falsey?

A

The falsey values are the empty string, 0, the keyword false, undefined, and null. In a boolean context, falsey values are treated as false (falsey values don’t necessarily equal false).

17
Q

[1, 89] === [1, 89];

var blue = [“da ba dee da ba die”];
var green = blue;
blue === green;

A

false

true

In this example, the === operator is comparing two variables that point to the same exact array, so true is returned.

18
Q

function blabbermouth() { };
console.log(blabbermouth.name);

A

“blabbermouth”
Functions have a name property that returns the name of a function. JavaScript functions are objects and can have properties like any other object.

19
Q

var dog = {
name: “joy”
}
console.log(dog.hasOwnProperty(“name”));

A

true

The hasOwnProperty() method returns true if the object has the property and false otherwise.

20
Q

const one = false || {} || null;
const two = null || false || ‘’;
const three = [] || 0 || true;

console.log(one, two, three);

A

{},’’,[]

With the || operator, we can return the first truthy operand. If all values are falsy, the last operand gets returned.