HTML and CSS Questions
What is semantic tags in HTML
Answer:
Difference between div and span
Answer:
Do you know specificity in CSS
Answer:
Difference between fixed and absolute in css
Answer:
Can we give height and width to inline element.
Answer:
Why we use important in CSS
Answer:
JS Questions
let and var variable difference
let is block scope
var is function scope
in let we can’t access value before declaration.
What is Hoisting?
Hoisting in JavaScript is a behavior in which a function or a variable can be used before declaration
// using test before declaring
console.log(test); // undefined
var test;
Variable Hoisting
In terms of variables and constants, keyword var is hoisted and let and const does not allow hoisting.
Is JS single-threaded or multi threaded.
JavaScript is a popular single-threaded programming language commonly used for web development. In web browsers, JavaScript runs in the main thread and handles tasks like user interactions, DOM manipulation, and HTTP requests.
How asynchronous work in js
In JavaScript, asynchronous operations are commonly handled using a combination of callbacks, Promises, and async/await syntax. Here’s how they work:
Callbacks: Callbacks are functions that are passed as arguments to other functions and are executed when the asynchronous operation completes. They’ve been traditionally used in JavaScript for handling asynchronous code. For example:
function fetchData(callback) {
setTimeout(() => {
callback("Data received");
}, 1000);
}
function processData(data) {
console.log(data);
}
fetchData(processData);
Promises: Promises provide a cleaner and more structured way to handle asynchronous operations compared to callbacks. A Promise represents a value that might not be available yet, but will be resolved at some point, either successfully or unsuccessfully. You can chain .then() and .catch() methods to handle the resolved value or error respectively. For example:
function fetchData() {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve("Data received");
}, 1000);
});
}
fetchData()
.then((data) => {
console.log(data);
})
.catch((error) => {
console.error(error);
});
Async/Await: Async functions allow you to write asynchronous code in a synchronous-like manner, making it easier to understand and maintain. You mark a function as async to indicate that it contains asynchronous operations, and you can use the await keyword to pause the execution of the function until a Promise is resolved. For example:
async function fetchData() {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve("Data received");
}, 1000);
});
}
async function processData() {
const data = await fetchData();
console.log(data);
}
processData();
Declarative and imperative in programing?
Difference between if else and ternary operators
- Use ternary operators to set a value to a variable, or to reduce code if necessary.
- Use if-else statements for everything else
- If-else is “statements”
- ternary operator is expression.
What is shallow copy and deep copy in JS?
Coding Questions
Output of below code of hosting.
var a = 20;
function foo() {
console.log(a);
var a = 10;
}
foo();
//Output: undefined
Out of below code of clouser.
function outer() {
function inner() {
console.log(x)
}
const x = 5
return inner
}
const inner = outer()
inner()
Here’s the breakdown of what happens:
- When
outer()is called and assigned toinner,innerbecomes a closure, capturing the scope ofouter. This means thatinnerretains access to all variables declared withinouter, even afterouterhas finished executing. - Inside
outer,xis declared and assigned a value of5. - Then,
inneris returned fromouter. - When
inner()is called, it tries to log the value ofx. Even thoughxis not directly declared withininner,innerhas access to the scope ofouter, including the variablex. - So, when
inner()executes, it successfully logs5.
Solve this issue
for(var i = 1; i <= 3; i++){
setTimeout(function() {
console.log(i);
},1000);
}
//issue is it output: 4, 4, 4
//Actual output required: 1, 2, 3
// Solution
for(let i = 1; i <= 3; i++){
setTimeout(function() {
console.log(i);
},1000);
}
// Change variation value to let
// Solution
for(var i = 1; i <= 3; i++){
(function(j) {
setTimeout(function() {
console.log(j);
},1000);
})(i);
}
// In this modified version, the value of i at each iteration is passed into the IIFE as a parameter j, which creates a new scope for each iteration, preserving the correct value of i for each setTimeout callback.
// https://chethanspoojary.com/blog/what-is-an-iife-in-javascript/
If I want to cause delay in the console
let time = 0
for(let i = 1; i <= 3; i++){
time = time + 1000;
(function(j, time) {
setTimeout(function() {
console.log(j, time);
},time);
})(i, time);
}
// we adding time + 1000
Without using time variable
for(let i = 1; i <= 3; i++){
(function(j) {
setTimeout(function() {
console.log(j);
},1000 * i);
})(i);
}