JavaScript Crash Course Flashcards
What are the JS primitive types?
Number
BigInt
Boolean
String
Symbol
Null
Undefined
What are the most common uses for the Math
object in everyday JavaScript development?
- Rounding Numbers:
-
Math.round():
Rounds numbers to the nearest integer.
-
- Rounding Down or Up:
-
Math.floor():
Rounds a number downward to the nearest integer. -
Math.ceil():
Rounds a number upward to the nearest integer.
-
- Generating Random Numbers:
-
Math.random():
Generates a random number between 0
(inclusive) and 1 (exclusive).
-
- Finding Maximum or Minimum:
-
Math.max():
Returns the largest of given numbers. -
Math.min():
Returns the smallest of given numbers.
-
- Absolute Value:
-
Math.abs():
Returns the absolute (non-negative) value of a
number.
-
- Exponentiation:
-
Math.pow():
Returns the base raised to the power of the exponent.
-
- Square Root:
-
Math.sqrt():
Returns the square root of a number.
-
What is parseInt()
in JavaScript, and how is it used?
It is used to parse a string and return an integer.
Note the string must begin with the number or have leading space. If it begins with a non numeric character it will return NaN.
parseFloat works the same way except it ignores the first ‘.’, so that it may return a floating number.
parseInt("23.5px")
// 23parseFloat("23.5px")
// 23.5
What is the alert()
function in JavaScript and how is it used?
The alert() function in JavaScript is used to display a popup alert with a specified message. For example, using alert("Hello, World!");
will show a popup with the message “Hello, World!”.
What is the console.log()
function in JavaScript and how is it used?
The console.log() function in JavaScript is used to output a message to the web console. For instance, if you use console.log("This is a log message.");
, it will print “This is a log message.” in the web console.
What is the .push()
method in JavaScript and how is it used?
The .push() method in JavaScript adds one or more elements to the end of an array. For example, if you have an array fruits and you use fruits.push("apple")
, “apple” will be added to the end of the fruits array.
What is the .pop()
method in JavaScript and how is it used?
The .pop() method in JavaScript removes the last element from an array. If you have an array fruits with the last element as “apple”, using fruits.pop()
will remove “apple” from the array
What is the .shift()
method in JavaScript and how is it used?
The .shift() method in JavaScript removes the first element from an array. For instance, if “apple” is the first element in the fruits array, using fruits.shift()
will remove “apple” from the array.
What is the .unshift()
method in JavaScript and how is it used?
The .unshift()
method in JavaScript adds one or more elements to the beginning of an array. For example, using fruits.unshift("apple")
will add “apple” to the beginning of the fruits array.
What is the .map()
method in JavaScript and how is it used?
The .map()
method in JavaScript creates a new array by calling a function on every array element. For instance, if you want to double every number in an array numbers, you can use numbers.map(x => x * 2)
What is the .filter()
method in JavaScript and how is it used?
The .filter()
method in JavaScript creates a new array with every element that passes a test. For example, to get only numbers less than 10 from an array numbers, you can use const arr = [10, 13, 8, 6, 9];
const newArr = arr.filter((e) => {
return e < 10;
})
returns: [8, 6, 9]
What is the .every()
method in JavaScript and how is it used?
The .every()
method returns a boolean that indicates if all the elements in an array pass a certain test. For example:const arr = [1, 2, 5, 125, 6]
const allPass = arr.every((e) => {
return e < 100;
}
returns false
because there is a value that is 100 or more in the array.
note: the .some()
method will check if any value passes
What is the .reduce()
method in JavaScript and how is it used?
The .reduce()
method in JavaScript reduces an array to a single value. For instance, to sum all numbers in an array numbers, you can use `numbers.reduce((accumulator, currentValue) => {
return accumulator + currentValue;
})’
What is the .forEach()
method in JavaScript and how is it used?
The .forEach() method in JavaScript calls a function once for each array element. For example, to log every element in an array fruits, you can use fruits.forEach(fruit => console.log(fruit))
What is the .find()
method in JavaScript and how is it used?
The .find()
method in JavaScript returns the value of the first array element that passes a test. const arr = [10, 13, 8, 6, 9];
const newArr = arr.find((e) => {
return e < 10;
}) returns: 8
note: findIndex()
will return the index of the found value instead of the value itself.
What is the .trim()
method in JavaScript and how is it used?
The .trim() method removes whitespace from both ends of a string. For example, " apple ".trim()
would return “apple”.
What is the .sort()
method in JavaScript and how is it used?
The .sort()
method in JavaScript sorts the elements of an array. For example, to sort an array fruits in alphabetical order, you can use fruits.sort()
.
What is the .charAt()
method in JavaScript and how is it used?
The .charAt() method returns the character at a specified index in a string. For example, "apple".charAt(3)
would return “l”.
What is the .indexOf()
method in JavaScript and how is it used?
The .indexOf() method returns the position of the first occurrence of a substring in a string. For instance, "apple".indexOf("p")
would return 1.
What is the .slice()
method in JavaScript and how is it used?
The .slice() method extracts a part of a string and returns a new string. For example, "apple".slice(1, 4)
would return “ppl”.
What is the .split()
method in JavaScript and how is it used?
The .split() method splits a string into an array of substrings based on a specified delimiter. For instance, "apple,orange".split(",")
would return [“apple”, “orange”].
What is the .replace()
method in JavaScript and how is it used?
The .replace() method replaces a specified value with another value in a string. For instance, "apple".replace("a", "o")
would return “opple”.
What is the .toUpperCase()
method in JavaScript and how is it used?
The .toUpperCase() method converts a string to uppercase letters. For example, "apple".toUpperCase()
would return “APPLE”.
What is the .toLowerCase()
method in JavaScript and how is it used?
The .toLowerCase() method converts a string to lowercase letters. For instance, "APPLE".toLowerCase()
would return “apple”.
What is the .substr()
method in JavaScript and how is it used?
The .substr() method extracts parts of a string, beginning at the character at the specified position, and returns the specified number of characters. For example, "apple".substr(1, 3)
would return “ppl”.
What is the .concat()
method in JavaScript and how is it used?
The .concat() method is used to join two or more strings. For instance, "apple".concat(" pie")
would return “apple pie”.
What is the .startsWith()
method in JavaScript and how is it used?
The .startsWith() method determines whether a string begins with the characters of a specified string. For example, "apple".startsWith("app")
would return true.
What is the .endsWith()
method in JavaScript and how is it used?
The .endsWith() method determines whether a string ends with the characters of a specified string. For example, "apple".endsWith("le")
would return true.
What is the .includes()
method in JavaScript and how is it used?
The .includes() method determines whether one string may be found within another string. For instance, "apple".includes("pp")
would return true.
What is the .repeat()
method in JavaScript and how is it used?
The .repeat() method constructs and returns a new string which contains the specified number of copies of the string on which it was called. For example, "apple".repeat(2)
would return “appleapple”.