Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
7 views

Javascript Cheatsheet Edx

Uploaded by

mnaveen1306
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Javascript Cheatsheet Edx

Uploaded by

mnaveen1306
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

6/17/24, 9:45 PM about:blank

JavaScript Cheatsheet

Item Syntax Description Example


var - global
access, value
can chage

let - access
within block
where it is let i = 5;
Declaring
declared,
Variables
let < var_name > = < value > value can var myStr = "John";
var, let,
change
const const pi = 3.14
const - access
within block
where it is
declared,
value cannot
change
Strings
length let myStr = "Hello";
Returns the console.log(myStr.length);
length string_obj.length
length of the
string Output is 5
split Splits the
string based let myStr = "Hello! How are you?";
on the console.log(myStr.split(" "))
split string_obj.split(separator)
separator and
returns an Output is [ ‘Hello!’, ‘How’, ‘are’, ‘you?’ ]
array.
charAt
returns the
character at a
let myStr = "Hello";<
specified
console.log(myStr.charAt(0))
charAt string_obj.charAt(index) index in a
string. Index Output is H
starts at 0
ends at length-
1
replace
searches a
string for a
specified
value, or a
let myStr = "Hello User";
regular console.log(myStr.replace("User","World"));
replace string_obj.replace("SearchValue","NewValue") expression,
and returns a Output is Hello World
new string
where the
specified
values are
replaced.
substring string_obj.substring(start, end) substring is let myStr="Hello";
used to extract console.log(myStr.substring(1,4));
characters,
Output is ell
between to
indices from
the given
string, and
returns the

about:blank 1/6
6/17/24, 9:45 PM about:blank
substring. It
excludes the
last index
startsWith
returns true if
a string begins let myStr="Hello from the other side";
with a console.log(myStr.startsWith("Hello"));
startswith string_obj.startsWith(searchvalue)
specified
string, Output is true
otherwise
false
endsWith
returns true if
a string ends let myStr="Hello from the other side";
with a console.log(myStr.startsWith("side"));
endsWith string_obj.endsWith(searchvalue))
specified
string, Output is true
otherwise
false
toUpperCase
let myStr="hello";
converts a console.log(myStr.toUpperCase());
toUpperCase string_obj.toUpperCase() string to
uppercase Output is HELLO
letters
toLowerCase
let myStr="HELLO";
converts a console.log(myStr.toUpperCase());
toLowerCase string_obj.toLowerCase() string to
lowercase Output is hello
letters
let myStr="Hello"; let str="World";
concat joins
console.log(myStr.concat(str));
concat string_obj.concat(string1, string2,..,stringN) two or more
strings. Output is HelloWorld
Arrays
push adds let myArr=["Hello"]; myArr.push("World");
new items to console.log(myArr);
push arr_name.push(value)
the end of an
array. Output is [“Hello”,”World”]
pop removes let myArr=["Hello","World"]; myArr.pop();
the last console.log(myArr);
pop arr_name.pop()
element of an
array. Output is [“Hello”]
length sets or
let myArr=["Hello","World"];
returns the console.log(myArr.length);
length arr_name.length number of
elements in an Output is 2
array.
indexOf
searches for a let myArr=["Hello","World"];
console.log(myArr.indexOf("World")
indexOf arr_name.indexOf(item) specified item
and returns its Output is 1
position.
lastIndexOf
returns the last let myArr=["Hello","World","Hello"];
index console.log(myArr.lastIndexOf("Hello");
lastIndexOf arr_name.lastIndexOf(item)
(position) of a
specified Output is 2
value.
entries arr_name.entries() entries const hello = ["h", "e", "l", "l","o"];
Returns and console.log(hello.entries());
Array Iterator
Output is
that helps you
to iterate Object [Array Iterator] {}
through the
array and
recieve each
entry as an

about:blank 2/6
6/17/24, 9:45 PM about:blank
array of two
elements
containing the
key and the
value, where
in the key is
the index
position of the
element and
value is the
element itself.
find Finds the
first
occurance of //Find the first string with s let myarr =
an element in ["Mercury","Venus","Earth","Mars"];
found = myarr.find(val=>{ return
let
Array.find(<arrElemet>=>{ //return boolean based
find on a condition }
the array val.includes("s"); }) console.log(found);
which returns
true on Output Venus
checking the
condition
filter Finds
the all
occurances of //Find the all strings with s let myarr =
["Mercury","Venus","Earth","Mars"]; let
elements in
Array.filter(<arrElemet>=>{ //return boolean found = myarr.filter(val=>{ return
filter based on a condition }
the array val.includes("s"); }) console.log(found);
which returns
true on Output [Venus,Mars]
checking the
condition
map
Processes the
all elements of let myarr =
["name","place","thing","animal"]; let
the array found = myarr.map(val=>{ return val+"s"; })
Array.map(<arrElemet>=>{ //return processed value
map }
which returns console.log(found);
a new
Output [ ‘names’, ‘places’, ‘things’,
processed ‘animals’ ]
array of same
size
let hello = ["hello", "world" ]; let lorem
concat = ["along","lorem"] let h =
concatenates hello.concat(lorem); console.log(h);
concat arr_name..concat(arr1.name);
(joins) two or
more arrays. Output is
[“hello”, “world”, “along”, “lorem”]
Map
set helps you
var newMap = new Map(); newMap.set("h", 1);
define a new
console.log(newMap);
set mapName.set(key,value); element with
akey and its Output is {“h” => 1}
value
get helps you var newMap = new Map(); newMap.get("h");
return a value console.log(newMap);
get mapName.get(key);
of key you are
searching for Output is Map(0) {size: 0}
get is used to
get all of the var newMap = new Map(); newMap.set("h",1);
newMap.set("i",2);
keys
keys mapName.keys(); console.log(newMap.keys());
associated
with the Output is {“h”, “i”}
mapName
values is used
to get all of var newMap = new Map(); newMap.set("h",1);
the values to newMap.set("i",2);
values mapName.values(); the keys console.log(newMap.values());
associated
with the Output is {1,2}
mapName

about:blank 3/6
6/17/24, 9:45 PM about:blank
has is used to
check if the var newMap = new Map(); newMap.set("h",1);
key passed newMap.set("i",2);
has mapName.has(key_name); resides in the console.log(newMap.has(i));
map or not,
and returns Output is true
true or false
delete is used var newMap = new Map(); newMap.set("h",1);
to delete the newMap.set("i",2); newMap.delete("h");
delete mapName.delete(key_name); key and the console.log(newMap);
value from the
map Output is {“i” => 2}
JSON
JSON is a
dictionary
let myjson1={}; let myjson2 =
Create JSON let varname={name1:value1,name2:values2,.....} Object with {"name":"Jennifer","age":"32"}
Key-Value
pairs.
Adds an entry
to JSON
Add entry to let myjson1 = {}; myjson1["name"]="Jason";
let jsonObj[<key>]=<value> Object
JSON console.log(myjson1);
mapping the
key to value
Operators
+ addition

- subtration

/ division
let num1 = 2; let num2 = 2;
* console.log(num1+num2); console.log(num1-
multiplication num2); console.log(num1/num2);
console.log(num1*num2);
Arithmetic <Operand1> <Operator> <Operand2>
%
console.log(num1%num2); num1++;
console.log(num1); num2--;
modulus(gives console.log(num1);
remainder)
Output is 4 0 1 4 0 3 3
++ increment
by 1

– decrement
by 1
&& (AND)is
used to check
if all the
operand
conditions are
true

|| (OR)is used let num1 = 12, num2 = 2;


console.log(num1>10 && num2>10);
to check if console.log(num1>10 || num2>10);
condition1 && condition2 condition1 || condition2
Logical ! condition1
either of the console.log(!(num1==num2));
operand
condition are Output is false true true
true

! (NOT) is
used to check
if the operand
condition is
not met
Assignment variable = value variable += incremental value a=b assigns let num1 = 12, num2 = 2;
variable -= decremental value %= modulus value /= the value of b console.log(num1=num2);
divide value *= multiply value console.log(num1+=num2); console.log(num1-
to a =num2); console.log(num1/=num2);
console.log(num1*=num2);
a+=b adds the console.log(num1%num2);
value of b to a console.log(num1=num2);
and stores it in
a Output is 2 14 10 6 24 0 2
about:blank 4/6
6/17/24, 9:45 PM about:blank
a-=b subtracts
the value of b
from a and
stores it in a

a%=b divides
the value of a
by b and
stores the
remainder in a

a/=b divides
the value of a
to b and stores
the quotient in
a

a*=b
multiplies the
value of a and
b and stores
the value in a
Loops
for loops
throughout the
block of code for(let num = 0 ; num <=5 ; num++){
for(initialization;condition;increment/decrement) a number of console.log(num) }
For Loop { //code block } times making
sure the Output is 0 1 2 3 4 5
condition is
satisfied
while itrates
through the let num1 = 0; let num2 = 5; while(num1 <
num2){ console.log(num1) num1++; }
block of code
while while(condition){ //code block } while a
specified
condition is Output is 0 1 2 3 4
true
do while let num = 5; do { console.log(num); num--;
loops }
throughout the while(num > 0)
do while do{ //code block } while(condition) block once
before
checking
condition. Output is 5 4 3 2 1
for in is used let arr = ["a","b","c"]; for(let i in arr)
to itrate { console.log(arr[i]); }
for (var in object) { //code block
through the
for in
} specific
property/type
of the object Output is a b c
Conditional statements
if a specified
let num = 5; if(num = 5){
condition is console.log(true); }
if if(condition){ //code Block... } true, a block
of code will Output is true
be executed
if a specified
condition is
true, a block
let num = 5; if(num = 4){ console.log(true)
of code will
if(condition){ //Code Block... } else { //Code } else { console.log(false) }
if-else Block... }
be executed.
in case of Output is false
false, else
block is
executed

about:blank 5/6
6/17/24, 9:45 PM about:blank
else if to let num = 10; if(num < 10){
specify a new console.log("number is smaller"); } else
if(condition){ //Code Block... } else if condition to if(num = 10) { console.log("number is
if-else if-else (condition) { //Code Block... } else { //Code test, if the equal"); } else { console.log("number is
Block... } first/previous greater"); }
condition is
Output is number is equal
false
switch to
select one of
many blocks
of code to be let num = 2; switch(num) { case 1:
console.log("Hello world!"); break; case 2:
switch(expression) { case <value1>: //code break; executed. And console.log("Hi"); break; default:
switch case <value2>: //code break; . . . default: break is used console.log("this is default"); }
//default code block } to end the
preocessing Output is Hi
within the
switch
statement.
Other useful operations
typeof
operator
returns a
string console.log(typeOf("Hello")) Output is
typeof typeof(operand)
indicating the "string"
type of the
unevaluated
operand
isNaN
determines
whether a
value is
isNaN isNaN(operand) anythying but console.log(isNaN("Hello")) Output is true
a number or
not. It returns
false for a
number
parseInt is a //0011 is 3 for binary, since binary only has 2
function that numbers 0, 1 the radix is 2
parses a string
argument and console.log(parseInt("0011", 2));
parseInt parseInt(string, radix) returns an //Default parseInt takes decimal system
integer of the console.log(parseInt("54"));
specified
radix.(radix is
a base) Output is 3 54
parseFloat is
a function that
parseFloat("3.14")
parses a string
parseFloat parseFloat(string)
argument and Output is 3.14
returns an
float

This cheatsheet covers the JS you will mostly use. To learn more commands you can go to this link.

Changelog
Date Version Changed by Change Description
25-09-2021 1.0 Lavanya T S Initial version created

© IBM Corporation 2021. All rights reserved.

about:blank 6/6

You might also like