This is a master course of JavaScript. In this course you can learn how the things works in JavaScript when to use and how to use it. All the topics are in readme format.
- HTML is the Language of Web Content.
- CSS is the Language of Web Style.
- JS is the language of That brings all these together.
- Vanilla JavaScript
- ECMAScript
- TypeScript
- ES6
- ES2015
- CoffeeScript
- Node js
- WebPack
- JSX
- Gulp
- Babel
- Web Pack
- Angular
- React
- Vue.js
Npm,Web Pack,Gulp are help applications.
- Modern Brower
- Code editor
- Live server
- The browser console
Install both on vscode
- Eslint:- Helps automatically detect coding errors and can do basic cleanup automatically.
- Prettier:- helps automatically cleanup your formatting. Both require Node.js,>Run npm install in terminal of vscode. file>prefferences>setting>search formatonsave>tick it.
- Use comments to describe your code
//Single line comment
/*
Multiline comment
*/
/**
*
*
*
*
*
*
*/- Define a functon and variable at top
- Use double or single quatation for string.
- Semicolons are not required.
- Check Tab size in vscode bottom ribbon.
- Use white space to make your code more redable browser dont have any issiue with white spaces.
- Comments are also used in debugging.
- Select code press CTRL + / (for comment).
We have to put the js file at the bottom so that it can access the html elements easily and manupulate them.But when we put it on head section compiler runs the html line by line and if it runs the js file it does not get the body element and show error to the console because our body is not loaded yet.
If you want to use JS in head section use "asunc" and "defer".Normally if you put the js in head section.
- HTML parsing.
- HTML parsing stop + Javascript Downloaded.
- Html parsing stop + Javascript Executed.
- Javascript Execuition Complete + Remaining HTML content is loaded.
The above is called as Content and rendering blocking.Because it blocks the rendering of HTML Content it slow down the website.
Browser downloads js in parallel while HTML renders.When JS is fully loaded,rendering stops while JS is executed.
- HTML parsing + download JS.
- Stop parsing HTML + executing JS.
- Start parsing HTML.
Browser downloads JS in parallel while HTML renders,then defer execution of JS until HTML renders in complete.
- HTML parsing + download JS.
- When HTMl parsing complete then at last start execuiting Js.
Async/defer is the standard way to load js today. Only use render blocking when you have a specific reason.Loading JS in the footer is now an anti-pattern.
Js modules allows us to break pieces of code from a javascript file and place it into separate file and then import them back into the original file again.
import person from "./info.js";
console.log(person);//info.js
const person={
//person information
};
export default person;You can not get person object in console because person object is only available for that info.js file.
- With javascript we are working with objects that are based on prototypes.
- JS deals with objects as we deals with the real world objects like a car or a bag all the object has its own property.
- Each object is a unique instance of an object prototype.
- like all the bags have two straps are common in all the bags and all are open at top.
- Methods:-property changing features inside objects.
- Eg :- same method to open and close the bag.
- Objects can contain the other object.
Javascript object is the collection of data and properties and methods that describes the objects and what it can do.
const obj={};
//curly brackets define the object
const obj1={
key:"value", //property defined as key-value pairs.
}- values can be any data type.
- Properties can nest sub-objects with their own properties.
- Methods are properties containing function.
- This keywords simply refers to the current object.
The object container somewhere to live.Foe this we use a container called variable or constant variable.
const box={}const is the constant variable. (box) is the name of the container. Whaterver is at RHS is placed at that box container.
We can change the properties of the object inside the container.We can't remove or replace object from the container.
const box={name:"magic box"}
box=5;//error its a consant
box.inside="apple"
console.log(box);
//{name: 'magic box', inside: 'apple'}
- Object properties are defined using a collon separated key-value pair.
- Key or name can be any string and it is placed at the left and the value can be any data-type.
- Name can contain letter,digit,$,underscore.
const box={};
console.og("This is a box:",box);- Dot notation:-is called dot-notation because you use the dot to separate the different property.
const car={
name:"bmw"
}
console.log(car.name);- Bracket notaion:-it allows to use more advance things and give us more control to access the property.
const car={
name:"bmw",
info:{
color:"red"
}
}
// 1st use
console.log(car["name"]);//BMW
//2nd use (we can use the variable to get the property of an object
let n="name";
console.log(car[n]);//BMW
console.log(name["info"]["color"]);//red//3rd use (We can use key of the property which not follow the naming convention).
const person={1:"first"}
console.log(person["1"]);//firstObject can contain contain their own function.These function typically perform actions on properties of object and when a function is inside in a object is called as method.
There are two ways to define a method.
const car={
name:"Sujuki",
firstMethod:function(a){
console.log(this.name)
}
}
car.firstMethod()//Sujukior
const car={
name:"Sujuki",
secondMethod(a){
console.log(this.name)
}
}
car.secondMethod()//SujukiExample1.
const car={
name:"Sujuki",
firstMethod(newName){
this.name=newName;
}
}
console.log(car.name)//Sujuki
car.firstMethod("BMW");
console.log(car.name)//BMWThe value is changed only on the user browser at that moment.It is used to control somethin like in game to move front and back by pressing keys. Example2.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<style>
body {
width: 100%;
height: 100vh;
display: flex;
justify-content: center;
align-items: center;
}
#box {
width: 300px;
height: 300px;
background-color: pink;
border: 1px solid black;
}
</style>
</head>
<body>
<div id="box"></div>
<script src="script.js"></script>
</body>
</html>// Select the box element
var box = document.getElementById("box");
var width = 0;
var height = 0;
// Add an event listener to the document object
document.addEventListener("keydown", function (event) {
console.log(event);
// Check if the right arrow key was pressed
if (event.keyCode == 39) {
// Change the background color of the box
// Increment the margin value by 100
width += 100;
// Set the new margin value to the box element
box.style.width = width + "px";
}
if (event.keyCode == 37) {
// Change the background color of the box
// Increment the margin value by 100
height += 100;
// Set the new margin value to the box element
box.style.height = height + "px";
}
});or
// Define an object that represents the box element and its properties
var box = {
element: document.getElementById("box"),
width: 0,
height: 0,
color: "white",
// Define a method that changes the width of the box
changeWidth: function () {
// Increment the width by 100
this.width += 100;
// Set the new width to the box element
this.element.style.width = this.width + "px";
// Change the background color of the box
this.element.style.backgroundColor = "green";
// Change the text color of the box
this.element.style.color = "white";
},
// Define a method that changes the height of the box
changeHeight: function () {
// Increment the height by 100
this.height += 100;
// Set the new height to the box element
this.element.style.height = this.height + "px";
// Change the background color of the box
this.element.style.backgroundColor = "green";
// Change the text color of the box
this.element.style.color = "white";
}
};
// Add an event listener to the document object
document.addEventListener("keydown", function (event) {
// Check if the right arrow key was pressed
if (event.keyCode == 39) {
// Call the changeWidth method of the box object
box.changeWidth();
}
// Check if the left arrow key was pressed
if (event.keyCode == 37) {
// Call the changeHeight method of the box object
box.changeHeight();
}
});Creating object blueprint or template using classes.Classes work as an template for objects. If you create a object of class the object will automatically get the older properties and methods of the class. Means we make change on class property it will reflect to the every instance of the class (object that are created by using this class)
- class declaration : class Name{}
- class expression : const Name=class{}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="class.js" type="module"></script>
<script src="script.js" type="module"></script>
</head>
<body>
</body>
</html>class.js
class Person {
// Define a constructor method
constructor(name, age) {
// Assign the parameters to the properties of the object
this.name = name;
this.age = age;
}
// Define a method named greet
greet() {
// Print a greeting message using the name property
console.log("Hello, I am " + this.name);
}
}
export default Person;script.js
import Person from "./class.js";
// Create a new object with the Person class
var person1 = new Person("Alice", 25);
// Access the properties and methods of the object
console.log(person1.age); // 25
person1.greet(); // Hello, I am Alice
- declare a class before use.
- Place your class in separate file and import from there.
- There another short or less advance way for doing the same thing like a class.
- Its "object constructor function".
- Like class object constructor function uses a capitalized name.
- It produces the new object by capturing the property of newly created objects.
Example1
// constructor function
function Person(first, last, age, eye) {
this.firstName = first;
this.lastName = last;
this.age = age;
this.eyeColor = eye;
}
// create two objects
var person1 = new Person("Alice", "Smith", 25, "blue");
var person2 = new Person("Bob", "Jones", 30, "brown");
// access properties
console.log(person1.firstName); // Alice
console.log(person2.lastName); // JonesExample2.
// constructor function
function Person(first, last, age, eye) {
this.firstName = first;
this.lastName = last;
this.age = age;
this.eyeColor = eye;
this.nationality = "English"; // add a property
this.name = function() { // add a method
return this.firstName + " " + this.lastName;
};
}
// create two objects
var person1 = new Person("Alice", "Smith", 25, "blue");
var person2 = new Person("Bob", "Jones", 30, "brown");
// access properties and methods
console.log(person1.nationality); // English
console.log(person2.name()); // Bob Jones- Class is a newer syntax introduced in ES2015, while constructor function is an older syntax that has been available since the beginning of JavaScript.
- Methods are defined separately in the class but in object constructor function methods are defined with this keyword.
- Both class and constructor function can be used to create multiple objects with the same properties and methods.
- Both class and constructor function can use the new keyword to invoke them and create object instances.
// class syntax
class Animal {
// constructor method
constructor(name, type, sound) {
this.name = name; // instance property
this.type = type; // instance property
this.sound = sound; // instance property
}
// instance method
makeSound() {
console.log(this.name + " says " + this.sound);
}
// static method
static isAnimal(obj) {
return obj instanceof Animal;
}
}
// create two objects using class
var dog = new Animal("Rex", "dog", "woof");
var cat = new Animal("Luna", "cat", "meow");
// access properties and methods using class
console.log(dog.name); // Rex
console.log(cat.type); // cat
dog.makeSound(); // Rex says woof
cat.makeSound(); // Luna says meow
console.log(Animal.isAnimal(dog)); // true
console.log(Animal.isAnimal(cat)); // true
// constructor function syntax
function Animal(name, type, sound) {
this.name = name; // instance property
this.type = type; // instance property
this.sound = sound; // instance property
// instance method
this.makeSound = function() {
console.log(this.name + " says " + this.sound);
};
}
// static method
Animal.isAnimal = function(obj) {
return obj instanceof Animal;
};
// create two objects using constructor function
var dog = new Animal("Rex", "dog", "woof");
var cat = new Animal("Luna", "cat", "meow");
// access properties and methods using constructor function
console.log(dog.name); // Rex
console.log(cat.type); // cat
dog.makeSound(); // Rex says woof
cat.makeSound(); // Luna says meow
console.log(Animal.isAnimal(dog)); // true
console.log(Animal.isAnimal(cat)); // trueconst rightNow = new Date();
console.log(rightNow);//Tue Oct 03 2023 11:26:08 GMT+0530 (India Standard Time)
console.log(rightNow.toDateString());//Tue Oct 03 2023
//Date is a object created with a constructor function and .toDateString() is a method defined in it?to inject new html in to the HTMl we need to get an element from the HTML then we are able to inject in it. Entire document is an object in browser.
console.log(typeof document)//objectWhen a browser renders a document it creates a document object model of that document.So that we can access DOM using Javascript.
let a="world";
document.body.innerHtml=`hello ${a}`;
//change the body contentBackticks tells the browser that anything inside me is HTML or Javascript.
Js expression is written inside dollorsign with curly brackets (${js}) .By doing this you only have to make changes in js and is reflected into HTML DOM directly.
We can also do the same thing with using simple string but it makes the things much more complicated so that Template literals comes in existance.
let a="kumar";
document.body.innerHTML='<h1>Abhinish' + a + '</h1>';DOM stands for Document Object Model. It is a way of representing a web page as a tree of objects that can be accessed and modified by a programming language like JavaScript. For example, you can use JavaScript to change the text, color, style, or position of any element on the web page by using the DOM.
DOM describes the herarchical model in the document.
The queryselector and queryselectorAll both take a css query as the parameter it puts in quataition mark.
//1 Select element main
document.querySelector('main');
//<main>…</main>
//2 Select h1 inside main
document.querySelector('main h1');
//<h1>Hello my name is abhinish kumar and i am from ludhiana</h1>
//3 Select last element of the list
document.querySelector('main ul li:last-child');
//<li>"hello 5"</li>
//4 Select all the list item (NodeList)
document.querySelectorAll('main ul li');
//NodeList(5) [li, li, li, li, li]Change the color of all the lsit
//1 Select all the list item and change the color of all the list item red and lst item with blue
let nodeList = document.querySelectorAll('main ul li');
nodeList.forEach(li=>{
li.style.color="red";
li=nodeList[nodeList.length-1];
li.style.color="blue"
})//1 If element contain single class name(get class name)
document.querySelector('h1').className;
//'heading'
//2 Change class name
document.querySelector('h1').className="updatedh1"
//"updatedh1"
document.querySelector('h1').className;
//"updatedh1"//1 spani has 3 classes (get all the classses)
document.querySelector('span').classList;
//DOMTokenList(3) ['first', 'second', 'third', value: 'first second third']
//2 Add new class
document.querySelector('span').classList.add("iamnew");
//undefined
document.querySelector('span').classList
//DOMTokenList(4) ['first', 'second', 'third', 'iamnew', value: 'first second third iamnew']
//3 Remove a class
document.querySelector('span').classList.remove("first");
//undefined
document.querySelector('span').classList
//DOMTokenList(2) ['second', 'third', value: 'second third']
//4 Toggle class name
document.querySelector('span').classList.toggle("switch");
//false
document.querySelector('span').classList.toggle("switch");
//true
document.querySelector('span').classList.toggle("switch");
//false
document.querySelector('span').classList.toggle("switch");
//true
// 5 Replace a class name
document.querySelector('span').classList.replace("third","newThirs");
//false
document.querySelector('span').classList
//DOMTokenList(3) ['first', 'second', 'newThirs', value: 'first second newThirs']Anything inside a tag is called Attribute.
//1 get all the attributes of a element
document.querySelector('img').attributes;
//NamedNodeMap {0: src, 1: alt, src: src, alt: alt, length: 2}
//2 Check element has attribute or not
document.querySelector('img').hasAttribute('src');
//true
//3 Get attribute value
document.querySelector('img').getAttribute('src');
//'img.png'
//4 Set a new attribute
document.querySelector('img').setAttribute("alt","newSet");
//undefined
document.querySelector('img')
//<img src="img.png" alt="newSet">
//5 remove any attribute
document.querySelector("img");
//<img src="img.png" alt="newSetttttttt">
document.querySelector("img").removeAttribute("alt");
//undefined
document.querySelector("img");
//<img src="img.png">You can also set the class as attribute and id ass well.
//1 get all styles of p
document.querySelector("p").style
//CSSStyleDeclaration {0: 'color', 1: 'background-color', accentColor: '', additiveSymbols: '', alignContent: '', alignItems: '', alignSelf: '', …}
//2 get a specific style of an element
document.querySelector("p").style.color;
//'red'
//3 Change a specific style
document.querySelector("p").style.color="blue";Insted of hyphen we use camelcase for css elements
//1 Append at last
let a=document.createElement('h1');
a.innerHTML=`<h1>Hello</h1>`;
document.querySelector("main").append(a);
//2 Append at beginning
let a=document.createElement('h1');
a.innerHTML=`<h1>Hello</h1>`;
document.querySelector("main").prepend(a);Apppend child is also common thing but it return the element.
Try it
- replaceChild.
- insertBefore.
- insertAdjacentElement.
Being able to name things is crutial for communication. In our code we have bunch of object. Name is used to create a vertual container to put the object within. In js we can put any data type to this containers. Single equalto means the object we have on the RHS we have to put it in the container that we have on LHS.
Arrays are like a block of container we can imagine it as a train with its boxes every box is connected with its corresponding box with serial number.In javascript we can put any type of value in arrays box or container eg:number,string,boolean,object or array. Arrays are object in javascript.
//Arrays - Declaration
let arr = ["apple","banana","cherry"];
//Access array elements
console.log(arr[0]); //"apple"
//It can accept any data type
let obj={name:"piyush"};
arr[3]=obj;
console.log(arr[3]);//{name:"piyush"}
console.log(arr[3].name);//"piyush"
//We can also get the length of the array
console.log(arr.length)//3Why array acctually do have methods? Methods are functions that we call on objects with dot notation.
arr.split();here arr is object and split is a method that we call on arr object.
So over all we know that arrays are objects in javascript because of having methods.And methods are functions that are attached to all the arrays that we create.
Slice method is used to extract a part from an array without changing the original array.
Slice method is also used for strings.
let arr=['a','b','c','d','e'];
console.log(arr.slice(2));
//Output is :-['c','d','e'];here all the elements of array having index number 2 or more is split from the array(arr) and created a new array that we can see on output.
let arr=['a','b','c','d','e'];
console.log(arr.slice(2,4));
//Output is :-['c','d'];2 is starting point and 4 is the ending point to split arr.But here index 4 is not included.So that we can get the elements from index 2 to 3 only.
let arr=['a','b','c','d','e'];
console.log(arr.slice(-2));
//Output is :-['d', 'e'];-ve sign is used to get the data from the last index. -1 is mostly used to get the last element of the array.
Q1.print all the elements of the array except first and last element.
let arr=['a','b','c','d','e'];
console.log(arr.slice(1,-1));
//Output is :-['b', 'c', 'd'];Q2.Can we create a copy of a array using slice? yes
let arr=['a','b','c','d','e'];
let x=arr.slice()
console.log(x);
//Output is :- ['a', 'b', 'c', 'd', 'e'];or we can also copy array using spread operator
let arr=['a','b','c','d','e'];
let x=[...arr];
console.log(x);
//Output is :- ['a', 'b', 'c', 'd', 'e'];Note:you can use anyone to clone a array.
Splice is also like slice but it returns a part of the original array by mutating it. Means we can get a part of arrary but our original array also changes while doing this. The first parameter of splice method is similar to the slice.
let arr=['a','b','c','d','e'];
console.log(arr.splice(2));//['c', 'd', 'e']
console.log(arr);//['a', 'b']it is mostly used to delete last element of the array.Like in calculator and in add to cart functionality.
let arr=['a','b','c','d','e'];
console.log(arr.splice(-1));//['e']
console.log(arr);//['a', 'b', 'c', 'd']Second parameter is for deletecount functionality. that deletes the number of elements that we pass in it.
let arr=['a','b','c','d','e'];
console.log(arr.splice(1,3));//from 1st index delete next 3 elements
//['b', 'c', 'd']
console.log(arr);//['a', 'e']Third parameter is the element that we put at the place of deleted array. Q1 Insert Apple at index 1
let arr=['a','b','c','d','e'];
console.log(arr.splice(1,0,"Apple");//[]
console.log(arr);//['a', 'Apple', 'b', 'c', 'd', 'e']Used to reverse the array
let name=['a','b','h','i','n','i','s','h'];
console.log(name.reverse());
//['h', 's', 'i', 'n', 'i', 'h', 'b', 'a']
console.log(name);
//['h', 's', 'i', 'n', 'i', 'h', 'b', 'a']let namea='Noor';
console.log(namea.split('').reverse().join(''));
// rooNlet a=[1,2,3,4,5];
let b=[6,7,8,9,10];
console.log(a.concat(b));
//[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]There is also another way that can concat tha arrays.
let a=[1,2,3,4,5];
let b=[6,7,8,9,10];
console.log([...a,...b]);
//[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]In this method we pass the separator to join the array elements with that separator. Join method returns a new string of combined array elements.
let a=[1,2,3,4,5];
let b=[6,7,8,9,10];
console.log([...a,...b].join('-'));
//1-2-3-4-5-6-7-8-9-10It does not print the undefined and null array elements.
The at() method of Array instances takes an integer value and returns the item at that index, allowing for +ve and -ve integers. Negative integers count back from the last item in the array.
//Syntax :- at(index)- Find the element of the array at any position
const arr=[1,2,3,4,5,6,7,8,9,10];
//with bracket notation
console.log(arr[0]);//1
//By using at()
console.log(arr.at(0));//1Both work same so why we use the at() method. The at() method is equivalent to the bracket notation when index is non-negative. Means by using at method you can get the elements with -ve index number.
- Find the last element of an array.
const arr=[1,2,3,4,5,6,7,8,9,10];
//1st way
console.log(arr[arr.length-1]);//10
//2nd
console.log(arr.slice(-1));//return arr [10]
console.log(arr.slice(-1)[0]);//10
//3rd
console.log(arr.at(-1));Note:-If you want to get only last element use at(); And if you want to apply chaining method to array use the method of slice and bracket notation. 3. It also works with "string"
console.log('abhinish'.at(1)); //b
console.log('abhinish'.at(-1)); //h//fins the highest bank transaction
const tranctions=[4,34,53,75,2,6,654,75,77,3430,49];
function findha(ht){
let re;
for(let i=0;i<ht.length;i++){
if(ht[i]>ht[i+1]){
re=ht[i];
}
}
return re;
}
console.log(findha(tranctions));//3430- ForEach is higher order function that acceps a callback function in order to what to do with the elements of the array.
- ForEach function call the function that is passed in it by itself.
- Callback functions are the functions that tells the higherorder functions what to do.
- Higher order functions are the functions that acceps the another functions as an arguments and return the function as its returning value.
//By using for of
const movements=[200,450,-400,300,-650,-130,70,300];
for(const movement of movements){
if(movement>0){
console.log(`You deposit ${movement}`);
}
else{
console.log(`You withdraw ${Math.abs(movement)}`);
}
}
//Output:
You deposit 200
You deposit 450
You withdraw 400
You deposit 300
You withdraw 650
You withdraw 130
You deposit 70
You deposit 300//By using forEach
const movements=[200,450,-400,300,-650,-130,70,300];
movements.forEach(function(movement){
if(movement>0){
console.log(`You deposit ${movement}`);
}
else{
console.log(`You withdraw ${Math.abs(movement)}`);
}
})
//Output:
You deposit 200
You deposit 450
You withdraw 400
You deposit 300
You withdraw 650
You withdraw 130
You deposit 70
You deposit 300How it works
- in first iteration 0:function(200) function is called with 200.
- in second iteration 1:function(450) function is called with 450.Tii the length of the array.
//By using for of
const movements=[200,450,-400,300,-650,-130,70,300];
for(const [i,movement] of movements.entries()){
if(movement>0){
console.log(`On ${i+1} day You deposit ${movement}`);
}
else{
console.log(`On ${i+1} day You withdraw ${Math.abs(movement)}`);
}
}
//Output:
On 1 day You deposit 200
On 2 day You deposit 450
On 3 day You withdraw 400
On 4 day You deposit 300
On 5 day You withdraw 650
On 6 day You withdraw 130
On 7 day You deposit 70
On 8 day You deposit 300//By using forEach
const movements=[200,450,-400,300,-650,-130,70,300];
movements.forEach(function(movement,index,array){
if(movement>0){
console.log(`On ${index+1} day You deposit ${movement}`);
}
else{
console.log(`On ${index+1} day You withdraw ${Math.abs(movement)}`);
}
})
//Output:
On 1 day You deposit 200
On 2 day You deposit 450
On 3 day You withdraw 400
On 4 day You deposit 300
On 5 day You withdraw 650
On 6 day You withdraw 130
On 7 day You deposit 70
On 8 day You deposit 300By using forEach its easy to do the same program.Because of the power of forEach function or method.
- The callback function accepts (currentElement,index,fullarray).
- Name of the parameter does not matter only matter its order.
- Use short names (mov,i,arr).
- Use this if you have to do the operation on array and with its index.
- When should you use for of and wen to use the forEach.
- You can not use continue break and continue statement.
- SO here you use for of loop to use the break and continue to come out of the loop.
For each is also avaliable for Maps and Sets.
const currencies = new Map([
['key','value'],
['USD','United States Dollar'],
['EUR','Euro'],
['GBP','Pound Sterling']
]);
// In map the first is key and second is the value.
currencies.forEach(function(value,key,map){
console.log(`${key}:${value}`);
})
//Output:
key:value
USD:United States Dollar
EUR:Euro
GBP:Pound Sterlingconst currenciesUnique=new Set(['USD','GBP','USD','EUR']);
console.log(currenciesUnique);
//Set(3) { 'USD', 'GBP', 'EUR' }const currenciesUnique=new Set(['USD','GBP','USD','EUR']);
currenciesUnique.forEach(function(value,key,set){
console.log(`${key}:${value}`);
})
//Output
USD:USD
GBP:GBP
EUR:EURThe key and the value is same why is it like that.Becaue a set doesnot have keys.
This is not a misteke this is intentially dome by developers to maintain the functionallity of the forEach meth with 3 arguments.it is like (value,value,set).
or (value,,set). indicates that its not necessary.
Project Bankist
Dom manupulation ------18
- Julie and kate are doing a study on dogs, so each of them asked five dog owners about their dogs age and store the data into an array (one array for each) For now they are just.Interested in knowing whether a dog is an adult or a puppy. A dog is an adult if it is at least three years old, and it's a puppy. If it's less than three years old. Create a function "checkdogs" Which asserts 2 arrays of dogs ages, ('dogsJulie' and 'dogskat').And it does the following
- Julie found out that the owners of the first and the last Two dogs actually have cats, not dogs. So creators shallow copy of Julies array and.Remove the cat edges from the.Copied array (because it's a bad practice to mutate function parameters).
- Create an array with both julies, corrected and kathe data.
- for each remaining dog load to the console where it's an adult ("dog One is an adult and is five years old') or a puppy ('Dog number 2 is still a puppy')
- Run the funciton for both test database.
Ans :-Js has a built in length property length.It returns the string as a number.
const success="Success";
const needLongPassword="Password should be atleast 8 charachters";
function findPassLength(password){
return password.length>=8?success:needLongPassword;
}
console.log(findPassLength("1234567"))
console.log(findPassLength("12345676744"))
\\ Password should be atleast 8 charachters
\\ Success2. Trim the empty spaces at the beginning and end of the string.(save password with no side white space).
Ans
const success="Success";
const needLongPassword="Password should be atleast 8 charachters";
function findPassLength(password){
console.log("before trim "+ password.length)
console.log("afterr trim" + password.trim().length)
}
findPassLength(" 1234567 ")
findPassLength(" 12345676744 ")
\\before trim 11
\\afterr trim7
\\before trim 15
\\afterr trim11Ans string.startsWith()
a. Returns a boolean (true or false) based on whether the search string is found. b. startsWith() has two parameters : the search string that we are looking for and the position where we want to start the search.
const success="valid";
const unvalid="Unvalid phone number";
function findPassLength(number){
return number.startsWith("+91")?success:unvalid;
}
console.log(findPassLength("+9188728198"))
console.log(findPassLength("+82827328"))
console.log(findPassLength("91827328"))
\\valid
\\Unvalid phone number
\\Unvalid phone numberAns: string.toLowerCase()
a. Return a new string in all lowercse letters --Does not modify the original string. b toLowerCase() does not taking any arguments.
function findPassLength(a="This Is my citY"){
return a.toLowerCase();
}
console.log(findPassLength())
\\this is my cityAns: string.toUpperCase().
a. Return a new string in all uppercase letters --Does not modify the original string b. toUpperCase() does not take any arguments.
function findPassLength(a="This Is my citY"){
return a.toUpperCase();
}
console.log(findPassLength())
\\this is my cityAns: string.includes()
a. Return a boolean based on whether or not the search string was found. b. includes() takes one required arguments, which is the string that is going to be searched for in the string the method is applied to.
const positive="positive";
const negative="negative";
function hasFun(message){
return message.includes("fun")?positive.toUpperCase():negative.toUpperCase();
}
console.log(hasFun("this is a funny video"))
console.log(hasFun("this is a bad video"))
\\POSITIVE
\\NEGATIVEAns: string.split();
a. Returns an array of strings based on the separator b. split() takes two arguments. --Separator-lets the method knows where you want to divide up the string. --Limit(optional) - specifies if you want to limit the number of entries in the array.
function splitEmil(emailList){
return emailList.split(", ");
}
console.log(splitEmil("apple.@gmail.com,ball.@gmail.com,cat.@gmail.com"))
\\[ 'apple.@gmail.com,ball.@gmail.com,cat.@gmail.com' ]Ans: string.slice() a. Return an extracted section of a string. --Does not modify the original string. b. slice() has two parameters: --indexStart(required)-index of the first character to include in the returned substring.
--indexEnd(optional)--index of the first character to exclude from the returned substring.
function getLastNumner(accNumber){
return accNumber.slice(5);
}
console.log(getLastNumner("123456789"))
\\6789Ans string.replace()
a. Returns a new string with the all the instances matched by the replacement text. --Does not modify the original string. b. replace() has two parameters: the pattern that needs to be replaced and the actual replacement.
function splitEmil(emailList){
return emailList.replace('gmail.com','npm.com')
}
console.log(splitEmil("apple.@gmail.com,ball.@gmail.com,cat.@gmail.com"))
\\apple.@npm.com,ball.@gmail.com,cat.@gmail.comAns: string.concat(); a. Return a new ,concatenated string --Does not modify the original string b. concat() can take as many parameters/strings as needed depending on what is going to be concatenated.
function splitEmil(fname="abhi",lname="kumar",other="Big b"){
return fname.concat(" ",lname," ",other);
}
console.log(splitEmil())Tim Berners-Lee is the inventor of the World Wide Web, which is a system of interlinked hypertext documents that can be accessed via the Internet. He created the first web browser, web server, HTML, URL, and HTTP in the late 1980s and early 1990s while working at CERN, a European research organization. He also founded and directed the World Wide Web Consortium (W3C), which oversees the standards and development of the Web. He is a professor at the University of Oxford and MIT, and a recipient of many awards and honors for his invention1
-
Sir Tim Berners-Lee invented the WWW in 1989.
-
All the internet is just a web of wires and wires has no any knowledge where to transfer the data.
-
So we need a protocol ,Its like a standard that every one follows.
-
HTTP is a protocol that allows the fetching of resources such as HTML document(thats why it is called as hypertext transfer protocol)
-
By the ombination of http and html we have www.
-
HTTP are the protocols or the rules that we use over the wire.
-
HTTP is the foundation of any data exchange on the web.
-
HTTP is a common language that a client and a server use to communicate.(we say to server can you give me this file server responses with that file).Because thy both understand their languanges.
-
The message send by the browser is called as request and the messages send by the server is called as response.
This language has only little words to communicate with the server
- GET:used to get the file from the server.
- POST:you post something in the server.
- PUT:used to ipdate the data on the server.
- Delete:used to delete a piece of data on the server.
example like in twitter get is used to get the twitter feed ,post is used to post your data or new user id to the server.put is used used to edit your tweet.Delete is used to delete your user account or a tweet.
Http is also used to get the video,audio and ajax to update our html page.
- 200 :successful.
- 400 :not found.
- 500 : server error.
your http request goes to the server and ask to the server to give you required file and if file not exist it will show you the http status 440 not found.
<form method="GET">
<register>Register now</register>
</form>Open the netword tool in devtoolbar. If you click the resiger button it will modify the link (?ada?adsa?adsasd?Adas) this is called a query string .it is the first way of sending the data to the server. The query string contains the (key value pair).
The second way of sending the data to server is through body.
<form method="POST">
<register>Register now</register>
</form>It does not create a query string on the link. It store the form data.
Think a person watching our communication it will get our password from query string. And if we send the data through body The hacker find the way to get your request to him first.See your password in the body of the form.
HTTPS means hypertext transfer protocol secure.The communication from server to browser is now encrypted.It means that only server and client has the key to open or to read this message.
The s in https uses a layer called transport security layer(secure socket layer) TSL and SSl for short. By using https even a third person get the request but he does not read this Because the password is now not in redable form.
Summary ,Your browser is a http client sending a request to the server machine. When a client clicks to a hyper link the browser builts an http request and sends it to the server to communicate now the server gives us the data as a response.
We can not send any type of data to server to communicate ,it can only be text. We cant not send a javascript object. JS object is very confusing for the errver because the server can be written in to java,php or in other language but string is understandable for every languages.
- JSON stands for javascript object notation.
- JSON is a syntax for storing and exchanging data.
- JSON is text,written with javascript object notation.
THERe is also another form XML. Both XML and JSON use to transfer the data over the server.
JSON uses object like syntax.and XML uses html like syntax.
- To send the data to the server we have to sonver our data in to JSON formet.and the server changes it to their own language to understand.
- And server then changes the request data to the JSON and send back to you.
- And to read the responsive you have to change the formed again.
Javascript comes with its own json functions to deal with json formet.
//json to object
//1. JSON.parse()
var obj=JSON.parse('{"name":"john","age":30}');
//object to json
//2. JSON.stringify()
var myJSON = JSON.stringify(obj);JSON is text and we can conver any javascript object to json and send json to the server.we can also conver any type of json data of server into a object.
Onevery time we make a request our page refresh ever for json request.
If you want to request any thing your whole page reload on every request. It causes a poor user experience.
We have AJAX that loads the data without disturbing the other part of your web page or without reloding.
Its a technology to combine pieces together,Its calles xml http request.
- it was very complecated before.
- its complexicity was handled by jquery thats why jquery was very famus.
- Now we have a FETCH in javascript for ajax call.
fetch('/my/url').then(response=>{
console.log(response);
});fetch does http for us.
Summry,when a event occur on a web page to login we send our detail to server and server responses that you are loged in and all these happen without refreshing the page with AJAX ,ajax is used to create a single page application.In SPA our page holds only required data and if we request to the new data AJAX vanish the previous data and rebuild that part with new data.
//fetch is a window object
//fetch is a api
fetch('https://jsonplaceholder.typicode.com/users')
.then(response=>response.json())
.then(user=>console.log(user));//console
fetch('https://jsonplaceholder.typicode.com/users')
//promise
fetch('https://jsonplaceholder.typicode.com/users')
.then(response=>console.log(response));
//fullfillFetch comes with their own .json() method to convert response formet.
Promises are new features in javascript.
Ans:- A promise is an object that may produce a single value some time in the future. Either a resolved value or a reason that its not resolved(rejected).
more:::A promise is a way of handling asynchronous operations in JavaScript, which means operations that take some time to complete and may not have a fixed outcome. For example, you can use a promise to request some data from a server, and then do something with the data when it arrives.
Ans:-A Promise can be in one of three states: pending, fulfilled, or rejected.
Ans:-You can create a Promise using the Promise constructor, which takes a single function (known as the executor function) and it defines what the promise will do and how it will resolve or reject.
more::::The executor function has two parameters: resolve and reject. These are also functions that you can call inside the executor function to indicate the outcome of the promise. If the promise is successful, you call resolve with the value that you want to return from the promise. If the promise fails, you call reject with the reason for the failure.
//Promise constructor
// create a new promise
let coinToss = new Promise(function(resolve, reject) {
// simulate a coin toss with a random number
let randomNumber = Math.random();
// if the number is greater than 0.5, resolve with "Heads"
if (randomNumber > 0.5) {
resolve("Heads");
}
// otherwise, reject with "Tails"
else {
reject("Tails");
}
});In this example, we create a new promise called coinToss that uses the Promise constructor with an executor function. Inside the executor function, we generate a random number between 0 and 1 using Math.random(). If the number is greater than 0.5, we call resolve with the value “Heads”. Otherwise, we call reject with the value “Tails”. This promise will either resolve with “Heads” or reject with “Tails” depending on the random number. To use the value or the reason of the promise, we need to attach handlers to it using the .then() and .catch() methods. For example:
// use the coinToss promise
coinToss
// attach a handler for success
.then(function(value) {
// log the value of the promise
console.log("The coin landed on " + value);
})
// attach a handler for failure
.catch(function(reason) {
// log the reason of the promise
console.log("The coin landed on " + reason);
});In this example, we use the coinToss promise and attach two handlers to it using .then() and .catch(). The .then() method takes a function that will be called if the promise resolves, and receives the value of the promise as an argument. The .catch() method takes a function that will be called if the promise rejects, and receives the reason of the promise as an argument.
The output of this code will be either “The coin landed on Heads” or “The coin landed on Tails” depending on the random number.
Ans:-You can use the .then() method to handle the successful resolution of a Promise and the .catch() method to handle any errors that occur.
You can chain Promises together using the .then() method, which returns a new Promise that resolves to the value returned by the callback function.
To chain multiple promises together, you can use the .then() method of the promise object. The .then() method takes a function as an argument, which will be called when the promise is resolved. The function can return another promise, which will be passed to the next .then() method in the chain. This way, you can perform multiple asynchronous operations in a sequence, where the output of one operation is used as the input for the next operation.
For example, suppose you want to fetch some data from a server, and then use that data to fetch some more data from another server. You can use promise chaining to do that:
// create a promise to fetch data from server A
let promiseA = fetch('https://serverA.com/data.json');
// chain a .then() method to handle the response
promiseA.then(function(response) {
// convert the response to JSON and return another promise
return response.json();
})
// chain another .then() method to handle the JSON data
.then(function(data) {
// use the data to fetch data from server B and return another promise
return fetch('https://serverB.com/data/' + data.id);
})
// chain another .then() method to handle the response from server B
.then(function(response) {
// convert the response to JSON and return another promise
return response.json();
})
// chain another .then() method to handle the JSON data from server B
.then(function(data) {
// use the data to do something else
console.log(data);
});n this example, we create a promise to fetch data from server A, and then chain four .then() methods to handle the responses and data. Each .then() method returns another promise, which is passed to the next .then() method in the chain. The final .then() method uses the data from server B to do something else.
This way, we can perform multiple asynchronous operations in a sequence, using promise chaining.
Ans:-You can use the .catch() method to handle errors that occur in a Promise chain.But it can catch the error only in the code that defines before it.In promise chaining.
A Promise chain is a sequence of asynchronous operations that are linked by the .then() method. Each .then() method takes a function that receives the result of the previous operation and returns a new Promise. This way, you can perform multiple tasks one after another, using the output of one task as the input for the next one.
For example, suppose you want to fetch some data from a server, parse it as JSON, and then display it on the web page. You can use a Promise chain like this:
fetch('/data.json') // returns a Promise that resolves to a Response object
.then(response => response.json()) // returns a Promise that resolves to the parsed JSON data
.then(data => {
// do something with the data, such as displaying it on the web page
console.log(data);
});However, sometimes things can go wrong in a Promise chain. For example, the server might be down, or the data might be invalid. In that case, you need to handle the errors gracefully and avoid crashing your program.
To handle any errors that may occur in the chain, you can add a call to .catch() at the end of the chain. If any of the Promises are rejected, this catch handler will run, and the rest of the chain is skipped1.
For example, suppose you want to handle any errors that might happen in the previous example. You can add a .catch() method like this:
fetch('/data.json') // returns a Promise that resolves to a Response object
.then(response => response.json()) // returns a Promise that resolves to the parsed JSON data
.then(data => {
// do something with the data, such as displaying it on the web page
console.log(data);
})
.catch(error => {
// handle any errors that might occur in the chain
console.error(error);
alert('Something went wrong!');
});This way, if any of the Promises in the chain are rejected, the catch handler will run and display an error message to the user.
You can also handle errors at any point in the chain by adding a second argument to the .then() method. This argument is a function that receives the error from the previous operation and returns a new Promise. This way, you can recover from some errors and continue with the chain2.
Promise.resolve() returns a Promise that is already resolved with a value, while Promise.reject() returns a Promise that is already rejected with an error.
// A promise that is resolved with the value 42 let promiseA = Promise.resolve(42);
// A promise that is rejected with the error “Something went wrong” let promiseB = Promise.reject(new Error(“Something went wrong”));The Promise.all() method takes an array of Promises and returns a new Promise that resolves when all of the input Promises have resolved.
// Create three promises that resolve after different delays let promise1 = new Promise((resolve, reject) => { setTimeout(() => { resolve(“First”); }, 1000); });
let promise2 = new Promise((resolve, reject) => { setTimeout(() => { resolve(“Second”); }, 2000); });
let promise3 = new Promise((resolve, reject) => { setTimeout(() => { resolve(“Third”); }, 3000); });
// Use Promise.all() to wait for all of them to finish Promise.all([promise1, promise2, promise3]).then((values) => { // This will run after all promises resolve console.log(values); // [“First”, “Second”, “Third”] }).catch((error) => { // This will run if any promise rejects console.error(error); });let urls=[
"https://jsonplaceholder.typicode.com/users",
"https://jsonplaceholder.typicode.com/photos",
"https://jsonplaceholder.typicode.com/albums" ];
Promise.all(urls.map(url=>{
return fetch(url).then(resp=>resp.json())
})).then(result=>{
console.log(result[0]);
console.log(result[1]);
console.log(result[2]);
}) // Add this parenthesisfetch api is a build in tool in browser used to fetch the data asynchronously from different netwirk requiest by using GET<POST<PUT fetch api contain first argument a http link and second argument is type of request.
console.log(fetch('https://reqres.in/api/users'));
>>>Promise{pending}Fetch is a promise based api
console.log(fetch('https://reqres.in/api/users'));
.then(res => console.log(res));
>>>>Response{data object}We are not able to access the body of returned data by using Response,we have to convert it into JSON formet.
console.log(fetch('https://reqres.in/api/users/1'));
.then(res =>res.json())
.then(data => console.log(data));
>>>>{data object}fetch will alwas succeed so to detect the uncessful we use
fetch('https://reqres.in/api/users/1')
.then(res =>{
if(res.ok){
console.log("SUccess");
}else{
console.log("Not Successful")
}
res.json()
})
.then(data => console.log(data));
>>>>{data object}Fetch is used to do some more operations with data,it can post the data delete the data from server and do a lot of things.