JavaScript Quiz Flashcards
What is the output?
function sayHi() {
console.log(name);
console.log(age);
var name = ‘Lydia’;
let age = 21;
}
sayHi();
undefined and ReferenceError
What is the output in the console?
+true;
!’Lydia’;
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.
let c = { greeting: ‘Hey!’ };
let d;
d = c;
c.greeting = ‘Hello’;
console.log(d.greeting);
‘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.
let a = 3;
let b = new Number(3);
let c = 3;
console.log(a == b);
console.log(a === b);
console.log(b === c);
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.
Is this valid?
function bark() {
console.log(‘Woof!’);
}
bark.animal = ‘dog’;
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.
let number = 0;
console.log(number++);
console.log(++number);
console.log(number);
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.
function getAge(…args) {
console.log(typeof args);
}
getAge(21);
The rest parameter (…args) lets us “collect” all remaining arguments into an array. An array is an object, so typeof args returns “object”
const a = {};
const b = { key: ‘b’ };
const c = { key: ‘c’ };
a[b] = 123;
a[c] = 456;
console.log(a[b]);
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.
What is the output?
!!null;
!!’’;
!!1;
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.
const fruit = [‘🍌’, ‘🍊’, ‘🍎’]
fruit.slice(0, 1)
fruit.splice(0, 1)
fruit.unshift(‘🍇’)
console.log(fruit)
[‘🍇’, ‘🍊’, ‘🍎’]
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 [‘🍇’, ‘🍊’, ‘🍎’].
let newList = [1, 2, 3].push(4);
console.log(newList.push(5));
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
const myFunc = ({ x, y, z }) => {
console.log(x, y, z);
};
myFunc(1, 2, 3);
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.
const name = ‘Lydia’;
age = 21;
console.log(delete name);
console.log(delete age);
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.
const list = [1 + 2, 1 * 2, 1 / 2];
console.log(list);
[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.
console.log(‘I want pizza’[0]);
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().