Variables, Types, and Collections - 23% Flashcards

1
Q

array method - .sort

A

Sorts a-z by default w/no cb.
For numbers, need to pass cb:
For reverse, pass a cb that returns a neg num if the 1st is > the 2nd
ASC - use .sort( (a, b) => a - b)
DES - (or rev alphabetical), use .sort( a, b) => b - a)

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

array method - .forEach

A

Pass cb, the fn gets called for every element. Does not modify elements.
array1.forEach(element => console.log(element));
can also pass index (item, index)

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

array method - .map

A

Modify elements using a cb.
let modifiedArr = arr.map(function(element){
return element *3;
});

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

array methods - .pop, .shift

A

pop - removes last el of array and returns it
shift - removes first el of array and returns it

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

array methods - .splice, .slice

A

splice - removing, replacing, or adding els in specified positions. Modifies origin array.
slice - cuts out subset of array and returns it as a shallow copy, orig array is not modified

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

array methods - .push, .unshift

A

push - adds el to end of array
unshift - adds el to start of array

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

array methods - .every

A

Returns a Boolean value that represents whether all els pass the condition in the cb fn

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

array methods - .filter

A

Returns a shallow copy of a subset of the array, with only the els that pass the condition in the cb fn

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

array methods - .indexOf

A

Returns the first index at which a given el can be found, or -1

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

array methods - .reduce

A

Reduces the array to a single value by applying the cb to each el and passing the return to the next.
Takes cb w/2 args (accumulated val and the current val) and and initial value (0 below):
array1.reduce((acc, current) => acc + current, 0);

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

array methods - .reverse

A

Reverses the array in place, returns ref to same array

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

Data Types (8)

A

String, Number,
BigInt - create by appending n to a num, for numbers > 2^53-1
Boolean,
Null - nothing, empty, or value unknown
Undefined - variable is declared, but value is not assigned
Object - not primitive, used to store collections of data
Symbol - used to create unique identifiers for objects

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

Map object

A

collection of key/value pairs, the keys are unique
Const myMap = new Map( );
myMap.add(‘apple’, ‘red’);
myMap.get(‘apple’) // returns ‘red’
myMap.size( ) // returns 1
myMap.has(‘banana’) // returns false
myMap.delete(‘apple’) // removes apple/red pair from myMap
myMap.clear( ) // removes all pairs from myMap

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

Map obj v. reg Obj

A

Map - keys can be any type
Object - keys only strings or symbols
Map - easy to get size
Object - have to keep track manually
Map - iteration is insertion order
for (const [key, value] of myMap)

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

Set obj

A

Collection of values, similar to array, except all are unique.
Const mySet = new Set( );
mySet.add(‘orange’);
mySet.has(‘orange’) // returns true
mySet.size( ) // returns 1
mySet.delete(‘orange’) // removes orange from mySet
Can iterate in insertion order:
for (const item of mySet)

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

Set v Array

A

Set - can delete by value: mySet.delete(val);
Array - deletion by value is slow: (arr.splice(arr.indexOf(val), 1))
Array - value NaN cannot be found by indexOf
Set - only unique vals, don’t have to keep track of duplicates

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

Convert b/t Array and Set

A

Array.from(mySet) // creates array
[…mySet] // creates array
new Set(myArray); // creates set, removes duplicates

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

var, let, const

A

var - gets hoisted, can declare w/o initializing, can reassign
let - can declare w/o initializing, can reassign
const - must initialize at declaration, cannot reassign

19
Q

template literal

A

Strings declared w/backticks.
- can embed vars/JS in them
- line breaks are respected, no need for \n

20
Q

escape chars in strings

A

Use backslash \

21
Q

string / number conversion

A

Number(<string here>)
String(<num here>)

22
Q

Types of Numbers

A

Integers - whole nums, can be positive or negative

Floating point numbers (floats) - have decimals

NaN (Not a Number): usually the result of an undefined or unrepresentable operation like division by zero.

Infinity: represents mathematical infinity. Can be positive or negative..

BigInt: can store and operate on integers beyond the safe integer limit for Numbers (15 places). e.g. 9007199254740991n

Binary, Octal, and Hexadecimal Numbers: JS supports binary (base 2), octal (base 8), and hexadecimal (base 16) numbers. For example, binary: 0b1010, octal: 0o755, hexadecimal: 0xABC.

In JS all nums are technically stored as 64-bit floating-point numbers, regardless of whether they are integers or decimals. This can lead to some precision issues for very large or very small numbers.

23
Q

.toFixed(n) - Number method

A

Rounds a decimal to a specified number of places (n)

24
Q

% operator
** operator

A

% = Remainder (or Modulo) - Returns the remainder after you’ve divided the left number by the right
8 % 3 // 2

** = Exponent - raises left num to right power
5 ** 2 // 25
equiv to Math.pow(5, 2)

25
Q

Increment / Decrement operator

A

e.g. ++ or --
Can only use these on a var that represents a num, not on a num itself.

Used after the variable, returns the value THEN increments it.

Used before the variable, increments the value THEN returns it

26
Q

Math obj

A

Obj built into JS, has properties and methods for mathematical constants and functions.
.floor(), .ceil() - Rounds down/up for the closest whole num
.min(), .max() - Returns the min or max val of a comma separated list of nums as arguments.
.random() - Returns a random number between 0 and 1.
ex: to get a random number less than “max”: Math.floor(Math.random() * max);

27
Q

Date obj

A

Has many methods for setting, getting, and manipulating dates. It does not have any properties.

28
Q

Create a Date obj

A

const now = new Date( );
Can pass a string in many different forms - differs among engines but all support YYYY-MM-DDTHH:mm:ss.sssZ - only the year, month and day are required.
const xmas = new Date(“2023-12-25”);
const alsoXmas = new Date(2023, 11, 25); - yes 11, since 0 index

29
Q

Date obj methods

A

“set” methods,
“get” methods,
“to” - for returning string values from Date objects.
parse and UTC methods, for parsing Date strings.

Ex: xmas.getMonth() returns 11, and xmas.getFullYear() returns 2023.

The getTime and setTime methods are useful for comparing dates, since they return the num of ms since the epoch (1/1/1970)

30
Q

Type Coercion

A

Automatic or implicit conversion of values from one data type to another (such as strings to numbers).

const value1 = “5”; // string
const value2 = 9; // num
let sum = value1 + value2;

console.log(sum); // “59”

JS converts 9 to a string automatically.

“9” == 9 // JS finds this true, converts automatically to be same type for the comparison

JS uses Type Coercion to determine truthy and falsy values

31
Q

Falsy Values

A

null, undefined, false, NaN, 0 (The Number zero, also including 0.0, 0x0, etc),
-0 (The Number negative zero, also including -0.0, -0x0, etc.),
0n (the BigInt zero, also including 0x0n, etc. Note that there is no BigInt negative zero — the negation of 0n is 0n.),
“” (Empty string value, also including ‘’ and ``.),
document.all (The only falsy object in JavaScript is the built-in document.all)

32
Q

Truthy Values

A

examples:
true, { }, [ ], 42, “0”, “false”, new Date( ), -42, 12n, 3.14, -3.14, Infinity, -Infinity
Note that empty objects, empty arrays, the string zero, and the string false, are all truthy.

33
Q

JSON

A

JavaScript Object Notation:
A text-based format to represent object syntax, can be used independently from JS.
Mostly used for transmitting data in web applications.
Only properties (no methods).
Double-quotes around keys and values.

34
Q

JSON / obj conversion

A

JSON.parse - accepts JSON, returns obj - deserialization
JSON.stringify - accepts obj, returns JSON - serialization

35
Q

array.flat

A

myArr.flat( ) - defaults to depth of 1, otherwise provide a number
myArr.flat(2) - flattens to 2 levels.
myArr.flat(Infinity) - flattens all the way.

36
Q

.trim and .padStart, .padEnd

A

.trim - removes whitespace from start and end of string
.padStart/.padEnd - takes a number for length, and a char to add

37
Q

strict mode

A

JS modules and Classes are already in strict mode.
add “use strict”;
prevents accidentally creating global variables

38
Q

Number( )

A

When declared with ‘new’ keyword, creates an obj.
When declared w/o ‘new’, coerces the input to a number or NaN

39
Q

parseFloat, isInteger

A

parseFloat can be used by itself (global) or as a method of Number obj.
- converts a num that is represented as a string, into a floating point number.

.isInteger is a method of Number obj, returns boolean

40
Q

toUTCString, toISOString

A

Both show time in UTC zone

41
Q

Date.parse(date)

A

Takes string date, returns its timestamp

42
Q

Symbol coercion

A

Symbol type can’t be implicitly coerced to String, only explicitly.

43
Q
A