JS Operators and Methods Flashcards
not
!
or
||
and
&&
how do you create a combination of booleans?
() for example
true && (true || false)
will derive true
equal to
===
not equal to
!==
addition
+
subtraction
-
multiplication
*
division
/
exponent
**
modulo
%
plus equals
\+= example: x += y x = x + y incrementing x by y
minus equals
-=
times equals
*=
divide equals
/=
absolute value
Math.abs( )
round up
Math.ceil( )
round down
Math.floor( )
parsing an integer
parseInt( )
parsing a float
parseFloat( )
generate a number between 0 and 1
Math.random( )
greater than
>
greater than or equal to
> =
less than
less than or equal to
<=
accessing a character in a string
string[index]
note: the index is the number that corresponds to the location of the character in the string. strings start at 0 in javascript.
reassigning a string
leave off var, let or const and reassign the variable to the new value; e.g. var someVarable = "something"; someVariable = "reassigned string";
concatenate two or more strings
+
string interpolation
+ string interpolation +
get the length of a string
.length
get the last character of a string
[ .length - 1]
e.g. “string”[“string”.length - 1]
get a portion of a string
.substring(first index, last index]
i.e. “string”.substring(3, 4);
would derive “in”
find where a portion of the string exists in a string
.indexOf( )
i.e. “string”.indexOf(“in”);
would derive 3
if it doesn’t exist, it derives -1
turn a value into a string
.toString( )
escape characters
\
new line
\n
create an array
[ ]
access an element in an array
array name [index of element]
e.g.
var array = [1, 2, 3, 4];
var secondElement = array[1]; console.log('secondElement:', secondElement)
reassigning an element in an array
array name [index] = “new value”;
note: arrays are mutable
length of an array
.length
last element of an array
array name[array name.length - 1]
almost identical to getting the last element in a string
adding an element to the back of an array
.push( )
removing an element from the back of an array
.pop( )
adding an element to the front of an array
.unshift( )
removing an element from the front of an array
.shift( )
adding an element in general to an array
.splice(index, 0, adding element) e.g. var array = [1, 2, 4]; array.splice(2, 0, 3); derives 1,2,3,4
removing an element in general from an array
.splice(index, number of removing elements, adding elements if there are any)
determine if a value is an array
Array.isArray( )
taking parts of an array and creating a new array
.slice(start, end);
add an array to another array
.concat
first array.concat(second array);
transform an array into a string
.join( ) all elements added together into a string
.join(“ “) adds spaces
.join(“-“) would add a dash between each element
transform a string into an array
.split( ) all elements added together in an array
.split(“ “) all elements added individually in an array
.split(“-“)if elements have dashes, it will add them individually in an array
determine where in an array we can find a particular element
.indexOf( )