Js Code With Harry

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7

String methods:

.length function for string length

.toUpperCase()

.slice(2,5) parameters are must

.replace(“huzaifga”,”Huzaifa”)

Stringname.trim()---- removes empty spaces

Sringnaem.concat(another string name);;

Stringmethods.mdn on google for all string methods

.tostring();

.join(“-”)

.pop() will remove the last element of array same

.shift() // removes first letter of array

.unshift() // removes an element from start of array

.delete[i];

.concat[a,b,c,d,e]

.sort();modify original array

.splice(srart index,elements to remove,elements to add)// will add new elemets to array

Array.from(a);// this will make array from obj a

For of and for in loop

For(let I of num){

Console.log(i);

Map filter and reduce:


let num =[1,3,4,5,3,2];

num.map((value,index,array)=>{

console.log(value,index,array);
})

FILTER METHOD:
.reduce() will add the elements of array

Console methods:
Conclose.error(“this will print in red form”)

Console.assert(condition) // will returen true or false

Concole.clear()// to clear concole

Console.table(obj name) will print table

Console,warn(“”)

Conso;e.infro()

Console.time(label)

Console.timeEnd(label)

Confirm , alert and prompt:

Prompt for input from user and store in variable

alert("enter value of a ");

let a = prompt("enter here",662);

document.write(a);

Alert : to invoke mini window

alert("hello your script works");

confirm:

gives short message with ye or no option this will return true for yes and false for no
location.href=https://google.com

walking with dom


document.head
document.body

typeof document.title

document.documentElement(for full html docum,ent)

meta tag:
meta tag is child of head

document.element.childNodes()

elem.hasChildnodes()

Element only navigation:

b.firstElementChild

tables:
document.body.firstElement.child.firstElementChild

DOM:

Let ctitle =Document.getElementByClassName(“abc class”);

Ctitle.style.color=”red”

Let ctitle= getElementById(“id”);

GetElementByTagName(“s”)
Matches,closestand contains:
Console.logid1.matches(“.class”)

Element.closest(css)

Chapter 8
Console.dir will print as an object with its properties

while concole.log will print content

>innerHTML will return inner part odf document and .innerHTML will be used to replace the text in the
old html valid only for element nodes

outerHTML

attributes in html js:

let a =idname.getAttribute(“class”) // this will print the class name with respected id.

let a =idname.hasAttribute(“class”) // we will find that if this id has attribute class or not

element.setAttribute(“class”,”hidden”)

element.removeAttribute(“class”)

for custom attributes addition data-attribute name =”value”

for accessing data attributes tagid.dataset

for innerhtml insertion

let a = document.getElementByTagName(‘div’)[0]

a.innerHTML= a.innerHTML+ ‘<h1>hello I am inserted<h1>’;

for creating new div

let div= document.createElement(‘div’)


div.innetHTML=’<h1>hello<h1>’

a.appendChild(div)

insertAdjacantHTML(‘beforebegin’,<div class = “test”> beforebegin</div>);

insertAdjacantHTML(‘beforeend’,<div class = “test”> beforeend</div>);

insertAdjacantHTML(‘afterend’,<div class = “test”> afterend</div>);

insertAdjacantHTML(‘afterend,<div class = “test”> afterend</div>);

set interval and set timeout:

alert("hello");

let a= setTimeout(function(){

alert("i am inside the timeout function");

},2000)

clearTimeout(a) // this will clear time out

console.log(a);

dom events:

<body>

<div class ="container">

<button onclick="alert('hello ')"> Click Me </button>

</div>

<script src="script.js"></script>

Onmouseenter:
<body>
<div class ="container" onmouseenter="alert('mouse inside container')">

<button onclick="alert('hello ')let a =6 ; console.log(a)"> Click Me </button>

</div>

Onclick

Add event listener and remove event liostner:


Call back call back hell():

Promisis for call back functions:


Let promise = new promise (function(resolve,reject){

})

Promise.then((value)=>{

Console.log(value)

})

Reject(new Error(“I am error”))

Promise chaining in js:

Promise API:
6 primary method:

First method if all promises are resolved then it will give final result but incase any one is not resolved
then this will not proceed

Let Promise_all= Promise.all([p1,p2,p3])

Promise_all.then((value)=>{

Console.log(“hello all promises are done”)})


Incase we want top return settled promises result then

Let Promise_all= Promise.allSettled([p1,p2,p3])

Promise_all.then((value)=>{

Console.log(“hello all promises are done”)})

Promise.race jo pehly hojay wo dyga

Async Await:
We can put our code to run

Async will return promise the

Error handling in js try and catch:


Try{

Colsole.log(Rahul)

Catch(error){

Console.log(error)

Custom error error object:


Error.message

Error.name

Throw new error(“ali is not good”)

The Finally Clause

Finally{

This piece of code inside finally block will execute under anycondition

You might also like