JavaScript
JavaScript
There are various data types in js as in case of many other coding languages but js is somehow
more flexible but a bit tricky with its datatypes and scope issues .
Let
Const
Var
JS is dynamically typed as we don't have to declare variable type
Var pierces scope of block
To integer
Let a = Number(num)
parseInt(num)
ParseFloat(num)
To string
.toString()
String()
Now comparing 2 numbers is easy but if you miss the conversion the follwing may help
The == converts both the strings into number then checks if they are equal
But === is strict check it doesnot convert but checks
Mathematics functions
Arrays
Elements can be of different types
Shallow copy - heap
Deep copy - stack
Array produces shallow copy
Push add at last
Pop delete at last
Unshift insert at beginning
Shift delete at beginning
Slice does not change array and doesnot include last index
Splice changes the initial index and no of elements to be deleted
12345
Splice 123
Original array 45
Join concatenates array and produces string type
Array.of(a , b ,c ) creates an array of elements
Array from("hitesh") creates an array of each element
Spread most imp [...marvel_heroes , ...dc_heroes]
Spread is a better way of concatenation
Destructuring
Loops
For of
For(let e in apples)
For each
Array.forEach(e => console log e )
Map
users.map(user => user.name);
Specify a key for identification
Filter sorts out the data
DOM manipulation
document.queryselector
.getElementById('title')
.getAttribute('id')
.class
setAttribute(' parameter to be changed ie class or I'd or base that holds the value ' , ' new
parameter ' )
B.innerText shows on what is visible on website as many content are hidden by css
B.textContent print entire content without hiding anything that may not be visible in website
B.innerHTML shows inner tags also
Write in script tag after body tag all the programs codes you learnt
. FirstElementChild selects first child
.LastElementChild selects Last child
Parent.child.length as parent is collection but children are elements
.parentElement
Create an elemnet
.createElement
Apply I'd class name attributes
createTextnode then .append
.innerText does the same as append overirdes previous
But after all this it won't be visible
So we need document.body.appendChild('element created ') to make it visible on webpage
Events
Async code
Js is synchronous and single threaded
So we async await in order to attain synchronous function for asynchronous code
Async projects
setTimeout()
Purpose: Executes a function after a specified delay
clearTimeout()
Purpose: Cancels a previously set timeout
setInterval()
Purpose: Repeatedly executes a function at a specified interval (in milliseconds)
API
Fetch
Axios
Localstorage.getitem
Localstorage.setitem
Promise
Try catch
try {
console.log("Trying to run something...");
let result = 10 / 0;
} catch (error) {
console.log("Caught an error:", error);
} finally {
console.log("This will always run, whether an error occurs or not.");
}