javascript Flashcards
(685 cards)
What’s the output?
function sayHi() {
console.log(name);
console.log(age);
var name = ‘Lydia’;
let age = 21;
}
sayHi();
undefined and ReferenceError
Variables with the let keyword (and const) are hoisted, but unlike var, don’t get initialized.
What’s the output?
let c = { greeting: ‘Hey!’ };
let d;
d = c;
c.greeting = ‘Hello’;
console.log(d.greeting);
Hello
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.
What’s the output?
for (var i = 0; i < 3; i++) {
setTimeout(() => console.log(i), 1);
}
for (let i = 0; i < 3; i++) {
setTimeout(() => console.log(i), 1);
}
3 3 3 and 0 1 2
Because of the event queue in JavaScript, the setTimeout callback function is called after the loop has been executed.
What’s the output?
const shape = {
radius: 10,
diameter() {
return this.radius * 2;
},
perimeter: () => 2 * Math.PI * this.radius,
};
console.log(shape.diameter());
console.log(shape.perimeter());
20 and NaN
The value of diameter is a regular function, whereas the value of perimeter is an arrow function.
With arrow functions, the this keyword refers to its current surrounding scope, unlike regular functions!
What’s the output?
+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.
Which one is true?
const bird = {
size: ‘small’,
};
const mouse = {
name: ‘Mickey’,
small: true,
};
mouse.bird.size is not valid
In JavaScript, all object keys are strings (unless it’s a Symbol). Even though we might not type them as strings, they are always converted into strings under the hood.
What’s the output?
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
when we use the === operator (Strict equality 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.
What’s the output?
class Chameleon {
static colorChange(newColor) {
this.newColor = newColor;
return this.newColor;
}
constructor({ newColor = ‘green’ } = {}) {
this.newColor = newColor;
}
}
const freddie = new Chameleon({ newColor: ‘purple’ });
console.log(freddie.colorChange(‘orange’));
TypeError
The colorChange function is static. Static methods are designed to live only on the constructor in which they are created, and cannot be passed down to any children or called upon class instances.
What’s the output?
let greeting;
greetign = {}; // Typo!
console.log(greetign);
{}
It logs the object, because we just created an empty object on the global object! When we mistyped greeting as greetign, the JS interpreter actually saw this as:
global.greetign = {} in Node.jsIn order to avoid this, we can use “use strict”.
What happens when we do this?
function bark() {
console.log(‘Woof!’);
}
bark.animal = ‘dog’;
Nothing, this is totally fine!
This is possible in JavaScript, because functions are objects!
What’s the output?
function Person(firstName, lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
const member = new Person(‘Lydia’, ‘Hallie’);
Person.getFullName = function() {
return ${this.firstName} ${this.lastName}
;
};
console.log(member.getFullName());
TypeError
In JavaScript, functions are objects, and therefore, the method getFullName gets added to the constructor function object itself. For that reason, we can call Person.getFullName(), but member.getFullName throws a TypeError.
What’s the output?
function Person(firstName, lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
const lydia = new Person(‘Lydia’, ‘Hallie’);
const sarah = Person(‘Sarah’, ‘Smith’);
console.log(lydia);
console.log(sarah);
Person {firstName: “Lydia”, lastName: “Hallie”} and undefined
For sarah, we didn’t use the new keyword. When using new, this refers to the new empty object we create. However, if you don’t add new, this refers to the global object!
What are the three phases of event propagation?
Capturing > Target > Bubbling
During the capturing phase, the event goes through the ancestor elements down to the target element. It then reaches the target element, and bubbling begins.
All object have prototypes.
false
All objects have prototypes, except for the base object. The base object is the object created by the user, or an object that is created using the new keyword. The base object has access to some methods and properties, such as .toString
What’s the output?
function sum(a, b) {
return a + b;
}
sum(1, ‘2’);
“12”
JavaScript converts the number 1 into a string, in order for the function to make sense and return a value. During the addition of a numeric type (1) and a string type (‘2’), the number is treated as a string.
What’s the output?
let number = 0;
console.log(number++);
console.log(++number);
console.log(number);
0 2 2
The prefix unary operator ++:
Increments the value (number is now 2)
Returns the value (this returns 2)
This returns 0 2 2.
What’s the output?
function getPersonInfo(one, two, three) {
console.log(one);
console.log(two);
console.log(three);
}
const person = ‘Lydia’;
const age = 21;
getPersonInfo${person} is ${age} years old
;
[””, “ is “, “ years old”] “Lydia” 21
If you use tagged template literals, the value of the first argument is always an array of the string values. The remaining arguments get the values of the passed expressions!
What’s the output?
function checkAge(data) {
if (data === { age: 18 }) {
console.log(‘You are an adult!’);
} else if (data == { age: 18 }) {
console.log(‘You are still an adult.’);
} else {
console.log(Hmm.. You don't have an age I guess
);
}
}
checkAge({ age: 18 });
Hmm.. You don’t have an age I guess
JavaScript checks if the objects have a reference to the same location in memory.
The two objects that we are comparing don’t have that: the object we passed as a parameter refers to a different location in memory than the object we used in order to check equality.
What’s the output?
function getAge(…args) {
console.log(typeof args);
}
getAge(21);
“object”
The rest parameter (…args) lets us “collect” all remaining arguments into an array. An array is an object, so typeof args returns “object”
What’s the output?
function getAge() {
‘use strict’;
age = 21;
console.log(age);
}
getAge();
ReferenceError
With “use strict”, you can make sure that you don’t accidentally declare global variables. We never declared the variable age, and since we use “use strict”, it will throw a reference error.
What’s the value of sum?
const sum = eval(‘10*10+5’);
105
eval evaluates codes that’s passed as a string. If it’s an expression, like in this case, it evaluates the expression. The expression is 10 * 10 + 5. This returns the number 105.
How long is cool_secret accessible?
sessionStorage.setItem(‘cool_secret’, 123);
When the user closes the tab.
The data stored in sessionStorage is removed after closing the tab.
If you used localStorage, the data would’ve been there forever, unless for example localStorage.clear() is invoked.
What’s the output?
var num = 8;
var num = 10;
console.log(num);
10
With the var keyword, you can declare multiple variables with the same name. The variable will then hold the latest value.
What’s the output?
const obj = { 1: ‘a’, 2: ‘b’, 3: ‘c’ };
const set = new Set([1, 2, 3, 4, 5]);
obj.hasOwnProperty(‘1’);
obj.hasOwnProperty(1);
set.has(‘1’);
set.has(1);
true true false true
All object keys (excluding Symbols) are strings under the hood, even if you don’t type it yourself as a string.
Click here!
"; }; }