Show Menu
Cheatography

Javascript Cheat Sheet (DRAFT) by

Javascript cheat sheet

This is a draft cheat sheet. It is a work in progress and is not finished yet.

Variable types

Data types

var age = 18;
// number
var name = "­Jan­e";
// string
var truth = false;
// boolean
var sheets = ["HT­ML",­"­CSS­"­,"JS­"];
// array
var a; typeof a;
// undefined
var a = null;
// value null
Objects:
var student = {
firstName:"Jane",
lastName:"Doe",
age:18,
height:170,
fullName : function() {
return this.f­irs­tName + " " + this.lastName;
}
};
// object name
// list of properties and values
// object function
 
// object end
studen­t.age = 19;
// setting value
studen­t[a­ge]++;
// increm­enting
name = studen­t.f­ull­Name();
// call object function
 

Operators

 
Comparison Operators
Comparison operators compare two values and return a boolean value, either
true
or
false
.
==
Equal to
5==5; //true
!=
Not equal to
5!=5; //false
>
Greater than
3>2; //true
>=
Greater than or equal to
3>=3; //true
<
Less than
3<2; //false
<=
Less than or equal to
2<=2; //true
===
Strict equal to
5==='5'; //false
!==
Strict not equal to
5!=='5'; //true
 
Logical Operators
Logical operators perform logical operations and return a boolean value, either
true
or
false
.
&&
Logical AND
true && false; // false
||
Logical OR
true || false; // true
!
Logical NOT
!true; // false
? :
Ternary operator
Returns value based on the condition
(5 > 3) ? 'success' : 'error'; // "­suc­ces­s"
condition ? expres­sio­nIfTrue : expres­sio­nIf­False
 
Assignment operators
Assignment operators are used to assign values to variables.
=
Assignment operator
a = 7; // 7
+=
Addition assignment
a += 5; // a = a + 5
*=
Multip­lic­ation Assignment
a = 3; // a = a  3
-=
Subtra­ction Assignment
a -= 2; // a = a - 2
/=
Division Assignment
a /= 2; // a = a / 2
%=
Remainder Assignment
a %= 2; // a = a % 2
**=
Expone­nti­ation Assignment
a = 2; // a = a2
 
Arithmetic Operators
Arithmetic operators are used to perform arithmetic calcul­ations.
+
Addition
x + y
-
Subtra­ction
x - y
*
Multip­lic­ation
x * y
/
Division
x / y
%
Remainder
x % y
++
Increment (incre­ments by 1)
++x or x++
--
Decrement (decre­ments by 1)
--x or x--
**
Expone­nti­ation (Power)
x ** y
 
Bitwise Operators
Bitwise operators perform operations on binary repres­ent­ations of numbers.
&
Bitwise AND
|
Bitwise OR
^
Bitwise XOR
~
Bitwise NOT
<<
Left shift
>>
Sign-p­rop­agating right shift
>>>
Zero-fill right shift
 
String Operators
In JavaSc­ript, you can also use the
+
operator to concat­enate (join) two or more strings.
+
Concat­enation operator
 
Other Operators
,
evaluates each of its operands (from left to right) and returns the value of the last operand.
let x = 1;

x = (x++, x);

consol­e.l­og(x); // 2
delete
deletes an object's property, or an element of an array
delete x
typeof
returns a string indicating the data type
typeof 3; // "­num­ber­"
void
discards the expres­sion's return value
void(x)
in
returns true if the specified property is in the object
prop in object
instanceof
returns true if the specified object is of of the specified object type
object instanceof object­_type

Loopz

 

Global Methods

 

Math functions III. - Number Methods

isFinite()
a value is a finite number?
isInte­ger()
a value is an integer?
isNaN()
a value is Number.NaN?
isSafe­Int­eger()
a value is a safe integer?
toExpo­nen­tial(x)
Converts a num. into an expone­ntial notation
toFixed(x)
Formats a num. with x numb.s of digits after the decimal point
toLoca­leS­tring()
Converts a num. into a string, based on the locale settings
toPrec­isi­on(x)
Formats a num. to x length
toString()
Converts a num. to a string
valueOf()
Returns the primitive value of a num.

Math functions I.

Math.r­oun­d(x.y);
// x.5 x.4
Math.t­run­c(x.y)
// x
Math.c­eil(); Math.f­loor();
// 
Math.p­ow(x,y)
// xy
Math.s­ign()
// -x, 0, x  -1, 0, 1
Math.a­bs(-x);
// x
Math.s­qrt(x); Math.c­brt(y);
// √x ∛y
Math.e­xp(x)
// value of Ex
Math.m­in(­a,b­,c,d); and Math.m­ax(­a,b­,c,d);
Math.r­and­om();
Math.l­og(); Math.l­og2(); Math.l­og10();

Math functions II. - Angle functions

sin(x)

asin(x)

asinh(x)

sinh(x)
// sine of x (x is in rad.)
// arcsine of x, in rad.
// hyperb. arcsine of x
// hyperb. sine of x
cos(x)

acos(x)

acosh(x)

cosh(x)
// cosine of x (x is in rad.)
// arccosine of x, in rad.
// hyperb. arccosine of x
// hyperb. cosine of x
tan(x)

atan(x)


atan2(y,x)


atanh(x)

tanh(x)
// tang. of an angl
// arctang. of x as a numeric value btw. -PI/2 and PI/2 rad.
// arctang. of the quotient of its arguments
// hyperb. arctang. of x
// hyperb. tang. of a num.

String functions

length
str.length
Returns the number of characters in a string
substr­ing()
str.su­bst­rin­g(i­nde­xStart, indexEnd)
Returns a specified part of the string
slice()
str.sl­ice­(be­gin­Index, endIndex)
Extracts and returns a section of the string
substr()
str.su­bst­r(b­egi­nIndex, length)
similar to
slice()
, but the second parameter specifies the length of the extracted part.
replace()
str.re­pla­ce(­pat­tern, replac­ement)
replace a substr­ing­/pa­ttern in the string
replac­eAll()
str.re­pla­ceA­ll(­pat­tern, replac­ement)
Returns string by replacing all matching patterns
toUpperCase()
toLowerCase()
str.toUpperCase()
str.toLowerCase()
Returns upperc­ase­/lo­wercase repres­ent­ation of a string
concat()
str.co­nca­t(str1, ..., strN)
Concat­enates the arguments to the calling string
~repeat()
str.re­pea­t(c­ount)
Returns a string by repeating it given times
trim()
str.trim()
Removes whitespace from both ends of a string
padStart()
padEnd()
str.pa­dSt­art­(ta­rge­tLe­ngth, padString)
str.padEnd(targetLength, padString)
Pads a string at the start/end to a given length
charAt()
str.ch­arA­t(i­ndex)
Returns character at a specified index in string
charCo­deAt()
str.ch­arC­ode­At(­index)
Returns Unicode of the character at given index
~fromCh­arC­ode()
String.fr­omC­har­Cod­e(num1, ..., numN)
Returns a string from the given UTF-16 code units
~codePo­intAt()
str.co­deP­oin­tAt­(pos)
Returns the Unicode point value at given index
~fromCo­deP­oint()
String.fr­omC­ode­Poi­nt(­num1, ..., numN)
Returns a string using the given code points
split()
str.sp­lit­(se­par­ator, limit)
Returns the string divided into list of substring
- - Search Methods - -
indexOf()
lastIndexOf()
str.in­dex­Of(­sea­rch­Value, fromIndex)
str.lastIndexOf(searchValue, fromIndex)
Returns the first index/last index of occurrence of a value
search()
str.se­arc­h(r­egexp)
Searches for specified value in the string
match()
str.ma­tch­(re­gexp)
Returns result of matching string with a regex
matchAll()
str.ma­tch­All­(re­gexp)
Returns iterator of results matching with a regex
includes()
str.in­clu­des­(se­arc­hSt­ring, position)
Checks if given string is found inside a string
startsWith()
endsWith()
str.st­art­sWi­th(­sea­rch­String, position)
str.endsWith(searchString, length)
Checks if a string begins­/ends with a specified string
locale­Com­pare()
str.lo­cal­eCo­mpa­re(­com­par­eStr, locales, options)
Compares two strings in the current locale
- - Template Literals - -
${...}
Template literals provide an easy way to interp­olate variables and expres­sions into strings.

Booleans

 
Boolea­n(e­xpr­ession)
The
Boolean()
function is used to convert various data types to boolean values.
You can use the
Boolean()
function to find out if an expression is
true
.
See Comparison and Logical Operators.

Array Functions

// const array_name = [item1, item2, ...];    
// var firste­lement = array[0]
push() and unshift()
adds an element at the end/be­ginning of the array.
pop() and shift()
remove the last/first element from an array.
length
// var last = array[­arr­ay.l­en­gth-1]
returns the number of elements in an array
Arrays is objects...
typeof­(array) // object
...but arrays too.
Array.i­sA­rra­y(a­rray) // true

Objects

 

Events

 

Event listener

 
 

Useful links

Statements Reference

 

Date Reference

 

Regular expres­sions

 

Class Reference

 

Error Reference