akshayJavaScript
<script>
// 1. Write clouser function example and explain
{ /*closure is action that is inner function can have access to the outer
function variable as well all the variables
*/}
{ /*const outerFun =(a) =>{
let b= 10;
const innerFun =() =>{
sum= a+b;
console.log(`sum of two number is ${sum}. ` );
}
innerFun();
}
outerFun(5); */ }
{/*2. Write a currying and write example.
currying simply means evaluating function with multiiple arguments and decomposing them
into a sequence of function with a single argument
function Addition(a) {
return function(b) {
return function(c){
return a+b+c;
}
}
}
let res=Addition(2)(4)(7);
console.log(res) */}
//3. Write one array and find out last element from array.
{/*
let arry = [2, 4, 6, 8, 10, 12, 14, 16];
// 1 st method length property
//let lastElement=arry[arry.length-1];
//console.log(lastElement);
// 2 nd method slice method
//let lastElement=arry.slice(-1);
//console.log(lastElement);
// 3rd pop method
let lastElement= arry.pop();
console.log(lastElement)*/}
//4. Reverse the string without using if else.
{/*var str = "I am Class"
console.log(str)
var output = str.split('').reverse().join('');
console.log (output) */}
//5. What is callback and write example of callback.
{ /*any function that is passed as an argument is called callback function
is to excuted after another function has finished executed
const funA = () => {
setTimeout(function () {
console.log(`Welcome Fn A`);
funB();
}, 3000)
}
const funB = () => {
console.log(`Welcome Fn B`);
}
funA(); */}
//6. What is higher order function and write example .
{/*
when a function recevies another function as an argument or a function return
another function as output in that case function will act like HOC
function x (fn) {
fn();
}
function y () {
console.log( "Hellow from y");
}
x(y);
*/}
// x is higher order function
// y is call back function
//7. What is spread operator and write example and copy one object to another.
{ /*spread operator is introduce ES6
syntax is 3 dots followed by iterable Array
it extend the array into indiviual component , so it can be used expand the
array in place whre zero & more element are expected
// whats is spread oprator
const users= ['user1', 'user2'];
console.log('without spread operator')
console.log(users)
console.log(...users)
// spread operator with array
const team1= ['user1', 'user2']
const team2=[ 'user3']
// without using spread operator
const newTeam = team1.concat(team2);
console.log(newTeam)
// using spread operator
const myTeam = [...team1, ...team2]
console.log(myTeam); */}
//8. Write array and find out unique number and its counts .
{ /*var arr= [2,3,4,56,7,2,3,4,6,1];
var count ={}
for (let i = 0; i < arr.length; i++) {
var num = arr[i];
count [num] = count[num] ?count [num] +1 :1;
}
console.log(count); */}
//9. What is Promise asyn ,await and write one example.
{ /*promise are used to handle asynchronous function operation in JSON
async & await make promise easier to write
async makes a function return promise
await makes a function await for a promise
async function getData() {
let handlePromise = new Promise((resolve, reject) => {
setTimeout(() => {
resolve("All Done ");
}, 1000);
})
let x = await handlePromise;
console.log(x)
}
getData();
*/}
//10. What is prototype and write one example.
{ /*In JS object inherit properites from prtotype
A prototype is a blue print of an object
also alows use properties and mehtod on object even if
the properties and mehtod do not exsist on then current object
var arr= [];
arr.push(2);
console.log(arr) */}
//11. What is arrow function and write on example.
{ /*the function decleared without the function keyword called arrow function.
introduced in Es6 version of js.
they provide us with a new and shorter syntax for declaring function.
arrow function can only be used as a function expression.
hellow =() =>{
return "Hellow word";
} */}
//12. What is annonous function and write on example.
//Anonymous function allows all creation of functions which have no specified name
// can be in variable
//can be return in function
//can be pass in function
{ /* var a= function () {
connsole.log ("Akshay")
};
a();
var b = function (x,y)
{
console.log (x+ ''+ y)
}
b(10,20)
*/ }
//13. What different between let const var , show me example.
//var - if we dclare variable from var , then we can also declare
//it with same name and also reasign the value
// let- if we declare varible from let , then we can not delclare
// it again with same name but can reassign value
// const - if we declare varible from const , then we can not delclare
// it again with same name but can'nt reassign value
//var myName= ("Akshay")
//var myName= ("Naikanware")
{ /*let myName= ("Akshay")
console.log(myName);
const myAge= 25
console.log (myAge) */}
//14. What is literal object synatx ,write one example or code.
{/*
object literal is simply a key value pair data structure.
var user={
name:'Akshay',
age:26,
place:'Pandharpur'
}
console.log(`my name is ${user.name} my age is ${user.age} from ${user.place}`)
*/}
//15. Take one array of object and filter the array using property.
{ /*const arry= [1,2,3,4,5];
const arryFilter= arry.filter(num=>num >3);
console.log(arry);
console.log(arryFilter); */}
//16. Different way of creating objects. Write examples.
//different way of creating object
// 1 st way using object literal
{/*var user = {
firstName: "Akshay",
lastName: "Naiknaware",
getFullName : function(){
return this.firstName + '' + this.lastName
}
}
console.log(user.getFullName())
// 2. using constructor
function User( firstName,lastName , emailId ){
this.firstName= firstName
this.lastName= lastName
} */}
//17. Define different data types in Javascript and write example of each
{ /*//number
//let data= 8
//console.log( typeof data);
//string
//let user= "Navin"
//console.log(typeof user);
let bool= 5<6
console.log(typeof bool);
let user = null
console.log(typeof user);
let user1
console.log(typeof user1); */}
//18.What is features of ES6 and write example of each feature
{/*
1 . spread operator
const team1= ['user1', 'user2']
const team2=[ 'user3']
const myTeam = [...team1, ...team2]
console.log(myTeam);
2. let and const
let myName= ("Akshay")
console.log(myName);
const myAge= 25
console.log (myAge)
3.template literal
var user={
name:'Akshay',
age:26,
place:'Pandharpur'
}
console.log(`my name is ${user.name} my age is ${user.age} from ${user.place}`)
4. arrow function
hellow =() =>{
return "Hellow word";
} */}
//19. What are different method added on new array, write example.
//1. for each()- this helps us to loop over array items
{ /* const arry= [1,2,3,4,5];
arry.forEach(item =>{
console.log(item);
})
*/}
//2. includes() - this method check if array includes the item passed in the mehtod
{/*const arry= [1,2,3,4,5];
let getValuePresent = arry.includes(2); // return true
console.log(getValuePresent) */}
//3 . filter () - this mehtod creates new array only
// element passed condtion inside the provided finction
{ /*const arry= [1,2,3,4,5];
const arryFilter= arry.filter(num=>num >3);
console.log(arry);
console.log(arryFilter); */}
// 4. map () - This mehtod creates new array by calling the provided function is every element
{ /*const arry= [1,2,3,4,5];
const oneAdded= arry.map(num=> num +1);
console.log (oneAdded); //op- [2, 3, 4, 5, 6]
console.log(arry);// op- //[1,2,3,4,5];
*/}
//5.reduce()- this mehtod applies a function against an accumulator and each element
// in the array
{ /* const arry= [1,2,3,4,5];
const sum= arry.reduce ((total , value ) => total+ value ,0 );
console.log(sum)
*/}
//20. Take one array of string or numbers remove duplicate from array.
{/*
let array= ["js", 1,2, "js" , 1 , "sartup", 2, true , "startup"]
let uniqueArray= [];
for (let i = 0; i < array.length; i++) {
if (! uniqueArray.includes(array[i])) {
uniqueArray.push(array[i]);
}
}
console.log(uniqueArray) */}
//21. What is ananomous function and write one example and execute it.
//Anonymous function allows all creation of functions which have no specified name
// can be in variable
//can be return in function
//can be pass in function
{ /* var a= function () {
connsole.log ("Akshay")
};
a();
var b = function (x,y)
{
console.log (x+ ''+ y)
}
b(10,20)
*/}
//22. What is callstack ,how to see Calistack.
{/*The call stack is used by JavaScript to keep track of multiple function calls.
It is like a real stack in data structures where data can be pushed and popped and
follows the Last In First Out (LIFO) principle */}
//23. What is Microstack how to see that .
{/*A microtask is a short function which is executed after the function or program which created it exits
and only if the JavaScript execution stack is empty, but before returning control to the event loop being
used by the user agent to drive the script's execution environment.
*/}
//24. What is this in the function explain
{ /*
this keyword refers to an Object
which depend on how this is being invoked
*/}
// 25 Write clouser for adding 2 numbers
{ /*const outerFun =(a) =>{
let b= 10;
const innerFun =() =>{
sum= a+b;
console.log(`sum of two number is ${sum}. ` );
}
innerFun();
}
outerFun(5); */}
//26.What is JSON ? Write one example.
{/*JSON stand javascript object Notification
is used storing & transporting DataTransfer
also used data is sent from server to a web page
const jsonObject = [
{
studentId: 101,
studentName: "David"
},
{
studentId: 102,
studentName: "Mike"
},
{
studentId: 103,
studentName: "David"
},
{
studentId: 104,
studentName: "Bob"
}
]
var result = jsonObject.filter(
obj => obj.studentName == "David"
);
console.log(result); */}
//27. What is storages in Javascript write one example
{ /*
1. Local storge-
allows you to save key/ value pair in the value
stores data with no expiration Date
data is not deleted when browser is closed & availbale for futur sessionStorage
localStorage.setItem(name,'Akshay')
localStorage.getItem(name)
localStorage.clear()
2. Session Storage
allows you to save key/ value pair in the value
store data only one session Storage
data is deleted when browser is closed
sessionStorage.setItem('name','aksahy')
sessionStorage.getItem('name') */}
//28. What is callback helll? Why to avoid callback heal.
{/*Callback Hell is essentially nested callbacks
stacked below one another forming a pyramid structure
*/}
//29. What is promise chain write one example.
//Promise chaining: Promise chaining is a syntax that allows you to chain together multiple
//asynchronous tasks in a specific order.
{ /*new Promise((resolve, reject) => {
// asyncronos function
setTimeout(() => {
resolve(1);
}, 1000);
})
.then((result) => {
alert(result);
return result * 4;
})
.then((result) => {
alert(result);
return result * 2;
})
.then((result) => {
alert(result);
return result * 4;
}) */}
//30. Write example of nested Object.
// nested object
{/*var user = {
email: "[email protected]",
personelInfo :{
name:"Akshay",
address:{
vilage: "Bardi",
city:"Pandharpur",
country: "India",
}
}
};
console.log(user.personelInfo.city);*/}
//31. What is event loop in Javascript.
// Event loop: An event loop is something that pulls stuff out of the queue and places it
//onto the function execution stack whenever the function stack becomes empty
{/*function first (){
console.log("First Called")
}
function second () {
console.log("second Called")
setTimeout(()=>{
console.log("API data")
},3000)
}
function third (){
console.log("third Called")
}
first();
second();
third(); */}
//32. Find out even and odd numbers from array
// using if else
{ /*var x= 7;
if (x%2 ==0) {
//even
console.log( `${x} is Even `)
} else {
console.log( `${x} is odd `) */}
// using ternary oparator
var x=7
var res= x%2 ==0 ? "Even" : "odd";
console.log( `${x} is an ${res} number` );
</script>