Javascript Flashcards
Code for
Exponent or Power
(**) or Math.pow(baseNumber, powerNumber)
Example: 2 ** 5 = 32
For squared exponent or power will always be 2
(6 ** 2), (20 ** 2), (3 ** 2)
Find remainder
(%)
example: 11 % 2 = 1
finds the remainder
(Since 2 goes into 11, 5 times, the remainder is 1)
If using % 2 and remainder is 1, number is odd. If remainder is 0, number is even
Sum (Plus-equals) (Increment)
Minus-equals (Decrement)
Product (Times-equals) (Multiplier)
Divide-equals (Divider)
Sum (+=)
Example: 11 += 2 = 13
Decrement (-=)
Example: 11 -= 2 = 9
Product (*=)
Example: 11 *= 2 = 22
Divider (/=)
Example: 12 /= 2 = 6
Find Absolute Value
Math.abs()
Example: Math.abs(-295) = 295
Measures distance from 0
Answer will always be positive
Round Up
Math.ceil()
Example: Math.ceil(33.7) = 34
Rounds up
Round Down
Math.floor()
Example: Math.floor(33.7) = 33
Rounds down
Code for
Parsing an integer from a string
Number.parseInt()
Code for
Parsing a float from a string
Number.parseFloat()
Generate a random Number
Math.random() * (max - min) + min)
(leave () blank)
Example: Math.random() * (10 - 1) + 1 =
Random number between 10 and 1
Boolean Operator for
Not
(!)
Example: (!true = false), (!boolean = notBoolean)
Creates Opposite
Boolean Operator for
Or
(||)
Example: (true || true = true), (true || false = true)
(false || false = false)
Both variables must be false to return false
All else is true
Boolean Operator for
And
(&&)
Example: (false && false = false), (true && false = false)
(true && true = true)
Both variables must be true to return true
All else is false
Boolean Operator for
Equal to
(===)
Example: (true === true = true), (false === false = true)
(true === false = false)
Both variables must be the same to return true
All else is false
Boolean Operator for
Not equal to
(!==)
Example: (true !== false = true), (false !== true = true)
(true !== true = false)
Both variables must be different to return true
All else is false
Find a Character in a string
“string”[index]
Example: “computer”[3] = p
Gets a specific character in a string
index always starts at 0
Add together two or more strings without a space
Add together two or more strings with a space
(without a space)
“string1” + “string2”
Example: “comp” + “uter” = computer
(with a space)
“string1” + “ “ + “string2”
Example: “John” + “ “ + “Smith” = John Smith
Interpolate a string
+ parameters +
Example:
“We will go “ + hiking + “ on “ + Tuesday + “.”
=
we will go hiking on Tuesday
Get the length of a string
“string”.length
Example: “Apple”
“string”.length = 5
Tells you the length of the string
Get the last character of a string
“string”[“string”.length - 1]
Example: “banana”
“string”[“string”.length - 1] = a
Tells you the last character in the string
Get a portion of a string
“string”.substring(parameters)
Example: “Queue”
“string”.substring(1, 4) = ueu
Tells you a specific portion of a string
Starts at 0, ends before 2nd parameter
Find the beginning index of a portion of a string
“string”.indexOf(substring)
Example: “Quicksort”
“string”.indexOf(“sort”) = 5
Tells you the index where a substring starts
Starts at 0
Change not a string into a string
parameter.toString()
(fill in parameter), (leave () blank)
Example: 54.toString = “54”
converts parameter into single string
Accessing an element in an Array
Array[index]
Example:
[water, coffee, tea][2]
array[index] = tea
Tells you the element at the given index
Index starts at 0
Get the length of an Array
Array.length
Example:
[1, 3, 4, 5, 7]
array.length = 5
Tells you the number of elements in an array
Remove the last element of an array
array.pop()
(leave () blank)
Example: [1, 2, 3] array.pop() ; return array ; = [1, 2]
Tells you the last element in an array
Add an element to the front of an array
array.unshift(newElement)
Example: [1, 2] array.unshift(3) ; return array ; = [3, 1, 2]
Adds new element to front of array
Add an element to the back of an array
array.push(newElement)
Example: [1, 2] array.push(3) ; return array ; = [1, 2, 3]
Adds new element to back of array