JavaScript Cheatsheet CodeWithHarry
JavaScript Cheatsheet CodeWithHarry
JavaScript Basics
Set of JavaScript basic syntax to add, execute and write basic programming paradigms in
Javascript
On Page Script
External JS File
<script src="filename.js"></script>
Functions
function nameOfFunction () {
// function body
DOM Element
Output
console.log(a);
Conditional Statements
1/15
Home - CodeWithHarry
If Statement
if (condition) {
If-else Statement
If the condition for the if block is false, then the else block will be executed.
if (condition) {
} else {
Else-if Statement
if (condition1) {
} else if (condition2) {
Switch Statement
switch(expression) {
case x:
// code block
break;
case y:
2/15
Home - CodeWithHarry
// code block
break;
default:
// code block
For Loop
While Loop
while (condition) {
Do While Loop
A do while loop is executed at least once despite the condition being true or false
do {
i++;
} while (condition);
Strings
The string is a sequence of characters that is used for storing and managing text data.
3/15
Home - CodeWithHarry
charAt method
str.charAt(3)
concat method
str1.concat(str2)
index of method
Returns the index of the first occurrence of the specified character from the string else -1 if not
found.
str.indexOf('substr')
match method
str.match(/(chapter \d+(\.\d)*)/i;)
replace method
Searches a string for a match against a specified string or char and returns a new string by
replacing the specified values.
str1.replace(str2)
search method
str.search('term')
split method
4/15
Home - CodeWithHarry
str.split('\n')
substring method
str.substring(0,5)
Arrays
The array is a collection of data items of the same type. In simple terms, it is a variable that
contains multiple values.
variable
concat method
concat()
indexOf method
indexOf()
join method
join()
5/15
Home - CodeWithHarry
pop method
pop()
reverse method
reverse()
sort method
sort()
toString method
toString()
valueOf method
returns the relevant Number Object holding the value of the argument passed
valueOf()
Number Methods
JS math and number objects provide several constant and methods to perform mathematical
operations.
toExponential method
toExponential()
6/15
Home - CodeWithHarry
toPrecision method
toPrecision()
toString method
toString()
valueOf method
valueOf()
Maths Methods
ceil method
Rounds a number upwards to the nearest integer, and returns the result
ceil(x)
exp method
exp(x)
log method
log(x)
pow method
7/15
Home - CodeWithHarry
pow(x,y)
random method
random()
sqrt method
sqrt(x)
Dates
Date object is used to get the year, month and day. It has methods to get and set day, month,
year, hour, minute, and seconds.
getDate()
getDay()
getHours()
8/15
Home - CodeWithHarry
getMinutes()
getSeconds()
getTime()
Mouse Events
Any change in the state of an object is referred to as an Event. With the help of JS, you can
handle events, i.e., how any specific HTML tag will work when the user does something.
click
element.addEventListener('click', ()=>{
});
oncontextmenu
element.addEventListener('contextmenu', ()=>{
});
dblclick
9/15
Home - CodeWithHarry
element.addEventListener('dblclick', ()=>{
});
mouseenter
element.addEventListener('mouseenter', ()=>{
});
mouseleave
element.addEventListener('mouseleave', ()=>{
});
mousemove
element.addEventListener('mousemove', ()=>{
});
Keyboard Events
keydown
element.addEventListener('keydown', ()=>{
});
10/15
Home - CodeWithHarry
keypress
element.addEventListener('keypress', ()=>{
});
keyup
element.addEventListener('keyup', ()=>{
});
Errors
Errors are thrown by the compiler or interpreter whenever they find any fault in the code, and it
can be of any type like syntax error, run-time error, logical error, etc. JS provides some functions
to handle the errors.
Try the code block and execute catch when err is thrown
try {
catch(err) {
Window Methods
Methods that are available from the window object
alert method
11/15
Home - CodeWithHarry
alert()
blur method
blur()
setInterval
setInterval(() => {
// Code to be executed
}, 1000);
setTimeout
setTimeout(() => {
// Code to be executed
}, 1000);
close
window.close()
confirm
The window.confirm() instructs the browser to display a dialog with an optional message, and to
wait until the user either confirms or cancels
open
12/15
Home - CodeWithHarry
window.open("https://www.codewithharry.com");
prompt
Prompts the user with a text and takes a value. Second parameter is the default value
scrollBy
scrollTo
clearInterval
clearInterval(var)
clearTimeout
clearTimeout(var)
stop
stop()
13/15
Home - CodeWithHarry
Query/Get Elements
The browser creates a DOM (Document Object Model) whenever a web page is loaded, and with
the help of HTML DOM, one can access and modify all the elements of the HTML document.
querySelector
document.querySelector('css-selectors')
querySelectorAll
document.querySelectorAll('css-selectors', ...)
getElementsByTagName
document.getElementsByTagName('element-name')
getElementsByClassName
document.getElementsByClassName('class-name')
Get Element by Id
document.getElementById('id')
Creating Elements
Create new elements in the DOM
createElement
14/15
Home - CodeWithHarry
document.createElement('div')
createTextNode
15/15