JavaScript Flashcards
What are JavaScript’s primitive data types?
Boolean, BigInt, Null, Number, String, Symbol, Undefined
What does the Number data type include?
All real numbers (positive and negative integers and floating point numbers, +/-Infinity)
What is a string?
Strings are a list of characters in a specific sequence surrounded by single or double quotes.
What is a Boolean?
Primitive data type which has the values of true or false.
What does a value of null represent?
Represents the intentional absence of a value.
The value of undefined represents…?
Represents the absence of a value and is used to convey the value does not exist.
What is an Object?
Collection of related data as key : value pairs.
Which values evaluate as falsy?
0, -0, 0n (BigInt zero) empty strings, null, undefined, NaN, false
What is an object property?
A property is a member of an object that associates a key with a value.
What is a method?
A method is a function which is associated with an object’s property.
What do comparison operators return?
A Boolean value of true/false
<, >, <=, >=, ==, ===, !=, !==
Three ways to define a function in JavaScript:
Function declaration.
Function expression.
Arrow function (also known as arrow function expressions)
Key difference between a function declaration and function expression?
Function expressions are not hoisted and cannot be called before they appear in a program.
What is an anonymous function?
A function with no name property.
When would you use a do/while statement?
When you want the code block to execute at least 1 time.
Functions that always return a true or false value are referred to as…
Predicates
What is function composition?
Passing a function call as an argument to another function.
[].includes()
An array prototype method that determines if a value passed as an argument is an element of the calling array and returns true or false as appropriate.
[].slice()
An array prototype method which returns a portion of an array into a new array object selected from index start up to but not including index end. If no index end argument is given, the method will automatically slice to the final element of the array.
.splice()
Changes the contents of an array by removing or replacing existing elements and/or adding new elements. Returns an array of the deleted elements. Argument format is:
splice(startIndex, deleteCount, item1, item2, itemN)
.push()
Adds one or more elements to the end of an array and returns the new length of the array.
.pop()
Removes the last element from the array and returns the removed element.
.join()
Creates and returns a new string by concatenating all of the elements in the array. If no separator argument is passed the elements will be comma separated with no spaces between elements.
[].concat()
Merges two or more arrays and returns a new array.
[1, 2].concat([3])\\ returns a new array [1, 2, 3]
.shift()
Removes and returns the first element from an array
.unshift()
Adds one or more elements to the beginning of an array and returns the new length of the array.
.reverse()
Reverses the order of elements in an array and returns a reference to the same array. This method is destructive and mutates the original array.
.flat()
Array prototype method that returns a new array with the sub-array elements concatenated into the new array, to the specified depth.
.sort()
Sorts the elements of an array in place and returns the reference to the same array, now sorted. The default sort behavior is to sort the array in ascending order, converting the array elements into strings, then comparing them.
.reduce()
What is it used for?
What is the structure of implementing the following Array method?
This array method is used to effectively reduce the contents of an array to a single value.
The reduce method takes two arguments:
1) a callback function with two parameters, the first parameter represents the accumulator value and the second parameter represents the current value of the iteration. The return value of the callback function is used as the accumulator in the next invocation of the callback.
2) initial value for the accumulator.
[].reduce((accumulator, element) => accumulator + element, \*initial value for accumulator variable*\)
Array.isArray()
Determines whether a passed value is an array and returns a Boolean value as appropriate.
Array.from()
Creates and returns a new array from an iterable. Can be passed a function expression that performs an action on each element of the iterable as a second argument to get behavior similar to .map()
Array.from('fox')\\ returns [‘f’, ‘o’, ‘x’]
console.log(Array.from([1, 2, 3], x => x + x));// Expected output: Array [2, 4, 6]
Object.keys()
Returns an array of an object’s own enumerable property names
{}.hasOwnProperty(’propertyString’)
Determines whether an object has the specified property as its own property (as opposed to inheriting it) and returns an appropriate Boolean value.
How can you iterate through all of an object’s properties?
Use a for/in loop
for (let prop in obj) { // iterates through all object properties even those of an object's prototype }
Object.values()
Returns an array of an object’s own property values
Object.values({a : 1, b : 2}); // [1, 2]
Object.create()
Returns a new object that will inherit from an existing Object which is passed as the method argument.
Object.assign()
Copies all of a source object’s own properties into a target object and returns the modified 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
Object.freeze()
Prevents an object from being changed and returns the same object that was passed as an argument.
Object.is()
returns a Boolean value indicating whether two arguments are the same value.
What do logical operators return?
logical operators return the last evaluated value.
|| &&
.endsWith()
String prototype method which determines if the calling string ends with the characters in the specified string argument and returns an appropriate Boolean value.
.charAt()
What if no passed argument? What if argument is out of bounds?
String prototype method that returns a new string with the character located at the index passed as an argument. If no passed argument, default of index 0 is used. If argument is out of bounds, returns an empty string.
.substring()
String prototype method that returns part of the calling string from the start index up to but not including the end index, or to the end of the string if no end index supplied.
.split()
String prototype method which splits a string at a specified pattern and returns an array of substrings.
''.includes()
How many arguments?
String prototype method which determines if a provided substring can be found in the calling string and returns an appropriate Boolean value. Takes an optional second argument that specifies which index in the string to start looking for the substring.
.match()
Retrieves the result of matching a string against a regular expression and returns and array whose contents depend on the presence or absence of the global flag.
const paragraph = 'The quick brown fox jumps over the lazy dog. It barked.';
const regex = /[A-Z]/g; //regex object
const found = paragraph.match(regex);
console.log(found);
// Expected output: Array ["T", "I"]
''.indexOf()
String prototype method that returns the index of the first occurrence of the provided substring.
.replace()
String prototype method that returns a new string with one, some, or all matches of an argument replaced by the replacement argument. The behavior of this method is flexible depending on the arguments passed. For instance you can pass a Regexp object for the matching pattern and a string as a replacement argument or even a function which is invoked for every match and its return value used as the replacement text.
.padStart()
String prototype method returns a new string that is padded with another string as many times is needed to reach the desired length. The first argument is the length of string desired and the second argument is the string that will be used to pad the string to the desired length.
Math.random()
Generates a random number >= 0 and less than 1
Math.floor()
Returns a rounded down integer.
parseInt()
Parses a string and returns an integer
parseFloat()
Parses a string and returns a floating point number.
.toFixed()
Formats a number to the desired number of digits after the decimal point and returns a string representing the number. If no digit argument provided, method has a default value of zero digits.
What does it mean for a variable name to be idiomatic?
Variable names that are idiomatic are accurate, descriptive, understandable to the reader. Their name conveys an accurate meaning to the reader.
What would make a variable name illegal?
1) Cannot start with a digit
2) Cannot contain whitespace
3) Cannot contain any special characters
4) Cannot be identical to JavaScript’s reserved keywords
What are the three primary variable naming conventions?
1) camelCase - used for variable and function names
2) PascalCase - reserved for naming Constructor functions
3) SCREEMING_SNAKE_CASE - used for naming constants that represent unchanging configuration values in the program.
When do implicit type coercions occur?
When a non-strict operation of two of more values with differing data types occurs.
What happens when number values are compared with string values using the
==(loose equality) operator?
Anytime a number and string are compared with
==the string is coerced to a number.
What happens when a Boolean value is compared to any other data type which performs a ‘loose’ comparison?
Boolean values are always coerced to numbers!
Anytime an object is compared to a primitive value the object is coerced to a … and then compared. How does the coercion behavior for empty array [] objects and empty objects {} differ?
Primitive. Empty array’s are coerced to the primitive value of zero whereas empty objects are coerced to a string value of ‘[object Object]’
Anytime one of the operands of the binary + operator is a string…
The other operand will be coerced to a string and the values will be concatenated.
Any combination of these data types used with the binary + operator will result in what behavior? (List the data types!)
Any combination of Booleans, Null, Number, Undefined will result in the values being coerced to numbers and added together.
What happens if one of the operands of the binary + operator is an object?
Both operands are coerced to strings and concatenated.