JavaScript Flashcards

Deeply understand the JS language and the tools within it. (47 cards)

1
Q

String bit storage in JS

A

In JavaScript, strings are fundamentally stored as a sequence of 16-bit unsigned integer values, representing UTF-16 code units. This means each character in a JavaScript string is typically represented by a minimum of 16 bits (2 bytes). These are called code units.

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

charCodeAt() vs codePointAt

A

JavaScript’s charCodeAt method gives you a code unit, not a full character code. The codePointAt method, added later, does give a full Unicode character, so we could use that to get characters from a string.

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

Number bit storage in JS

A

JavaScript stores numbers as 64-bit double-precision floating-point numbers, adhering to the IEEE 754 standard. This means each number occupies 64 bits of memory.
These 64 bits are allocated as follows:
Sign bit (1 bit): Indicates whether the number is positive or negative.
Exponent (11 bits): Represents the power of 2 that the mantissa is multiplied by.
Mantissa/Fraction (52 bits): Represents the significant digits of the number.

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

bit vs byte

A

A bit is the smallest unit of data in computing, representing a single binary value (0 or 1). A byte is a larger unit, typically consisting of 8 bits. Bytes are commonly used to represent characters, and are the standard unit for measuring storage capacity.

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

|| operator

A

|| (Logical OR)
Returns the first truthy value (or the last one if none are truthy).

Commonly used for default values.

const name = userName || “Guest”;
If userName is falsy (like null, undefined, ‘’, 0, false), “Guest” will be used.

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

&& operator

A

&& (Logical AND)
Returns the first falsy value (or the last one if none are falsy).

Commonly used for conditional execution.

isLoggedIn && showDashboard();
If isLoggedIn is truthy, showDashboard() runs.

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

?? operator

A

?? (Nullish Coalescing)
Returns the right-hand value only if the left is null or undefined.

Safer for defaulting when ‘’, 0, or false are valid values.

const count = inputCount ?? 10;
If inputCount is null or undefined, 10 is used. If it’s 0, 0 is kept — unlike ||.

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

unary vs binary operators

A

Unary and binary operators differ in the number of operands they act upon. Unary operators take one operand, while binary operators take two.

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

expression

A

An expression evaluates to a value

https://www.joshwcomeau.com/javascript/statements-vs-expressions/

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

statement

A

A statement performs an action or task

https://www.joshwcomeau.com/javascript/statements-vs-expressions/

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

declaration

A

A declaration introduces a name (like a variable) and associates it with a value or type.

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

function expression

A

The function keyword can be used to define a function inside an expression.

const getRectArea = function (width, height) {
return width * height;
};

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

function declaration

A

The function declaration creates a binding of a new function to a given name. Function declarations are not part of the regular top-to-bottom flow of control. They are conceptually moved to the top of their scope and can be used by all the code in that scope.

function calcRectArea(width, height) {
return width * height;
}

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

arrow function expression

A

An arrow function expression is a compact alternative to a traditional function expression, with some semantic differences and deliberate limitations in usage:

Arrow functions don’t have their own bindings to this, arguments, or super, and should not be used as methods.
Arrow functions cannot be used as constructors. Calling them with new throws a TypeError. They also don’t have access to the new.target keyword.
Arrow functions cannot use yield within their body and cannot be created as generator functions.

const multiply = (x, y) => { return x * y; };
const square = x => x * x;
const horn = () => console.log(“Toot”);

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

generator functions

A

The function* declaration creates a binding of a new generator function to a given name. A generator function can be exited and later re-entered, with its context (variable bindings) saved across re-entrances.

function* generator(i) {
yield i;
yield i + 10;
}

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

.repeat()

A

The repeat() method of String values constructs and returns a new string which contains the specified number of copies of this string, concatenated together.

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

lexical scoping

A

In lexical scoping, when you write a function or block, it remembers the variables that were in scope at the time and place it was defined, not when it is called.

x = 10

def foo():
print(x)

def bar():
x = 20
foo()

bar()
Under lexical scoping (e.g., in Python):

foo prints 10, because when foo was defined, x = 10 was in scope.

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

dynamic scoping

A

In dynamic scoping, variable lookup happens based on the call stack at runtime, not based on where the code appears in the file.

x = 10

def foo():
print(x)

def bar():
x = 20
foo()

bar()

Under dynamic scoping:

foo prints 20, because bar() was the caller of foo, and bar() had x = 20.

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

How Does the Call Stack Work?

A

JavaScript operates in a single-threaded environment, meaning that it can only execute one operation at a time. The Call Stack in JavaScript manages the order of function execution, handling nested calls and recursion. It ensures functions run in the correct sequence and are properly returned to after execution. Improper use, like infinite recursion, can lead to a stack overflow. Understanding it is key to maintaining efficient program flow.

Function Call, Executing the Function, Nested Function Calls, Returning from a Function, Completion of Program

https://www.geeksforgeeks.org/javascript/what-is-the-call-stack-in-javascript/

20
Q

closures

A

A closure is the combination of a function bundled together (enclosed) with references to its surrounding state (the lexical environment). In other words, a closure gives a function access to its outer scope. In JavaScript, closures are created every time a function is created, at function creation time.

function makeAdder(x) {
return function (y) {
return x + y;
};
}

const add5 = makeAdder(5);
const add10 = makeAdder(10);

console.log(add5(2)); // 7
console.log(add10(2)); // 12

add5 and add10 both form closures. They share the same function body definition, but store different lexical environments. In add5’s lexical environment, x is 5, while in the lexical environment for add10, x is 10.

21
Q

Math.abs()

A

The Math.abs() static method returns the absolute value of a number.

22
Q

isNaN(undefined);

A

true

The original isNaN function was a mistake, and Number.isNaN should be preferred in all circumstances.

23
Q

Number.isNaN(undefined);

A

false

The original isNaN function was a mistake, and Number.isNaN should be preferred in all circumstances.

24
Q

Object.assign()

A

The Object.assign() static method copies all enumerable own properties from one or more source objects to a target object. It returns the modified target object. It does not create a new object; instead, it mutates the target directly and then returns that same target object.

const target = { a: 1, b: 2 };
const source = { b: 4, c: 5 };

const returnedTarget = Object.assign(target, source);

console.log(target);
// Expected output: Object { a: 1, b: 4, c: 5 }
console.log(returnedTarget === target);
// Expected output: true

25
static method
In JavaScript, a static method is a method that belongs to the class itself, rather than to instances (objects) of that class. This means you can call a static method directly on the class name without needing to create an object from that class first.
26
padStart()
The padStart() method of String values pads this string with a given string (repeated and/or truncated, if needed) so that the resulting string has a given length. The padding is applied from the start of this string. const str1 = "5"; console.log(str1.padStart(2, "0")); // Expected output: "05"
27
serialization
The process whereby an object or data structure is translated into a format suitable for transfer over a network, or storage (e.g., in an array buffer or file format). In JavaScript, for example, you can serialize an object to a JSON string by calling the function JSON.stringify().
28
property
A JavaScript property is a member of an object that associates a key with a value. A JavaScript object is a data structure that stores a collection of properties.
29
method
A method is a function stored as a property of an object, and it typically works with that object’s own data.
30
property descriptors
In JavaScript, property descriptors are objects that describe the characteristics of a property on an object. These characteristics define how the property behaves when you access, modify, enumerate, or configure it. There are two types of property descriptors: Data descriptors — describe a normal property with a value. -value: the actual value of the property. -writable: whether the property’s value can be changed. -enumerable: whether the property shows up in loops like for...in or Object.keys(). -configurable: whether the property descriptor itself can be changed or the property deleted. Accessor descriptors — describe a property with getter/setter functions. -get: a function called when the property is read. -set: a function called when the property is assigned a value. -enumerable: same as above. -configurable: same as above. Important: a property cannot have both value/writable and get/set at the same time. It’s either a data descriptor or an accessor descriptor.
31
property attributes
Every property in JavaScript has a set of attributes that determine its characteristics. The property descriptor exposes and allows you to control these attributes. The attributes (writable, enumerable, configurable, value/get/set) are fundamental to each property. If you imagine properties as little data boxes, the attributes describe how the box behaves, and the property descriptor is your tool to read or change those descriptions.
32
compiled language
Compiled languages are translated into machine code (or bytecode) before runtime. Compiled languages generally offer faster execution speeds. Languages: C, C++, Rust, Go
33
interpreted language
Interpreted languages are translated and executed line by line during runtime. Interpreted languages often provide greater flexibility and platform independence. Languages: Python, JavaScript, Ruby
34
.pop()
The pop() method of Array instances removes the last element from an array and returns that element. This method changes the length of the array.
35
.push()
The push() method of Array instances adds the specified elements to the end of an array and returns the new length of the array.
36
.shift()
The shift() method of Array instances removes the first element from an array and returns that removed element. This method changes the length of the array.
37
.unshift()
The unshift() method of Array instances adds the specified elements to the beginning of an array and returns the new length of the array.
38
.slice()
The slice() method of Array instances returns a shallow copy of a portion of an array into a new array object selected from start to end (end not included) where start and end represent the index of items in that array. The original array will not be modified.
39
.splice()
The splice() method of Array instances changes the contents of an array by removing or replacing existing elements and/or adding new elements in place.
40
1 in array;
For arrays, x in a asks whether the array has something in index x. For example, the array ['a', 'b', 'c'] has elements in indexes 0 through 2.
41
.map() in code
function map(array, transform) { let mapped = []; for (let element of array) { mapped.push(transform(element)); } return mapped; }
42
.reduce() in code
function reduce(array, combine, start) { let current = start; for (let element of array) { current = combine(current, element); } return current; }
43
prototype
Prototypes are the mechanism by which JavaScript objects inherit features from one another. https://developer.mozilla.org/en-US/docs/Learn_web_development/Extensions/Advanced_JavaScript_objects/Object_prototypes
44
classes vs functions
In JavaScript, class is syntactic sugar over regular functions, making object-oriented code cleaner and easier to manage. Unlike functions, classes are not hoisted, must be called with new, and always run in strict mode. Methods in classes are non-enumerable by default, and inheritance is simpler using extends and super. While regular functions require manual setup for inheritance, classes make it more readable and less error-prone. // Using class class Person { constructor(name) { this.name = name; } greet() { console.log("Hi, I'm " + this.name); } } // Equivalent with function function Person(name) { this.name = name; } Person.prototype.greet = function() { console.log("Hi, I'm " + this.name); };
45
private methods
declared with # beginning the variable name. These are not stored on this like public properties. Instead, they are part of a hidden, internal slot managed by the JavaScript engine.
46
map data structure
A map (noun) is a data structure that associates values (the keys) with other values. For example, you might want to map names to ages. It is possible to use objects for this. Object property names must be strings. If you need a map whose keys can’t easily be converted to strings—such as objects—you cannot use an object as your map. Fortunately, JavaScript comes with a class called Map that is written for this exact purpose. It stores a mapping and allows any type of keys. let ages = new Map(); ages.set("Boris", 39); ages.set("Liang", 22); ages.set("Júlia", 62);
47