Javascript Built In Methods Flashcards
String()
Turns primitive strings into objects
What is the difference between forEach, map, filter and reduce?
forEach does not transform or return any values and uses callbacks
map transforms the values in an array but does not ‘mutate’ or change the number of elements in an array
filter has a callback that returns a boolean which decides whether or not to include the original value in the result
reduce combines the values in an array to return 1 result
forEach
takes a function and applies that function into every item in an array
map
creates and returns a new array after it applies a function over every item in a collection
[1, 2, 3].map(x => x + 1) // [2, 3, 4]
map DOES NOT change the collection
filter
performs a test on each item in a collection. If it passes the test, then it is moved into a new array and returns only those items
What are the 2 ways you can convert a string into a number?
parseInt(value) //returns an integer or whole number
parseFloat(value) //returns a decimal value
What built in methods come with JavaScript string objects?
.length()
.toUpperCase()
.toLowerCase()
What are some common methods on the Math object in JavaScript?
Math.random() //returns a random number below the number passed
Math.ceil() //returns the highest value of a whole number
Math.floor() //returns the lowest value of a whole number
Math.round() //returns the rounded value
How do you get a random number between a range using Math.random()?
- Build a function that will generate a random number
- Pass a max and a min as your parameters
- Inside your function, ‘floor’ your result by wrapping it inside Math.floor
- Establish a range by subtracting your min from your max, adding 1 and add your min to the end
- Multiply your range with Math.random
Math.floor( Math.random( ) * ( max - min + 1 ) + min )