What is Callback function and example program?
A function that is passed as an argument to another function, to be “called back” at a later time.
Example
Function one(){
Console.log(“Function 1”);
Function two(){
Console.log(“Function 2”);
Function three(){
Console.log(“Function 3”);
One();
setTimeout(two, 2000);
three();
Cookies, localstorage, session storage?
Cookies: Files created by websites you visit.
Local Storage: Web application store the data locally within the user’s browser.
Session Storage: Data is lost when the browser tab is closed.
Username validates
Table insertion dynamically
function generateTable() {
// creates a <table> element and a <tbody> element
const tbl = document.createElement("table");
const tblBody = document.createElement("tbody");
// creating all cells
for (let i = 0; i < 2; i++) {
// creates a table row
const row = document.createElement("tr");
for (let j = 0; j < 2; j++) {
// Create a <td> element and a text node, make the text
// node the contents of the <td>, and put the <td> at
// the end of the table row
const cell = document.createElement("td");
const cellText = document.createTextNode(`cell in row ${i}, column ${j}`);
cell.appendChild(cellText);
row.appendChild(cell);
// add the row to the end of the table body
tblBody.appendChild(row);
// put the <tbody> in the <table>
tbl.appendChild(tblBody);
// appends <table> into <body>
document.body.appendChild(tbl);
// sets the border attribute of tbl to '2'
tbl.setAttribute("border", "2");
Date object DD-MM-YYYY
Call, apply and bind methods
call()->we can pass arguments separetly
function getName(){
console.log(this.fname+""+this.lname);
}
let user = {
fname: 'mahesh ',
lname: 'kosuru'
}
getName.call(user);
apply()->we can pass arguments in array
function employeedetails(city1,city2,city3){
console.log(this.fname +" "+ this.lname + " " + city1 + " " + city2 +
" " + city3);
}
let employee = {
fname: "Mahesh",
lname: "Kosuru"
}
let cities = ["hyderabad", "banglore", "mumbai"];
employeedetails.apply(employee, cities);
function employeedetails(city){
console.log(this.fname +" "+ this.lname + " " + city);
}
let employee = {
fname: "Mahesh",
lname: "Kosuru"
}
const bindName = employeedetails.bind(employee);
bindName("hyderabad");
Spread operator
(…) allows us to quickly copy all or part of existing array or object into another array or object.
let car1 = {
color: "Red",
name: "Ferrari",
model: "GTC4"
}
let car2 = {
type: "F1 race",
year: "2023",
number: "9999"
}
const cars = {...car1, ...car2};
console.log(cars);
Arrow function
It allows you to create functions in a cleaner way compared to regular functions.
hello = () => {
return "Hello World!";
}
Anonymous function
a function that does not have any name associated with it.
let show = function() {
console.log('Anonymous function');
};
show();
Promise, async and await
Random number
Hoisting
Prototype
Mutable and immutable
Adding element for array in first place
Closure
Currying