Frontend Developer Interview Questions and Answers
Ques 11. Explain the concept of 'hoisting' in JavaScript.
'Hoisting' is a JavaScript behavior where variable and function declarations are moved to the top of their containing scope during the compilation phase. However, only the declarations are hoisted, not the initializations.
Example:
```javascript
console.log(x); // Outputs: undefined
var x = 5;
```
Ques 12. What is the difference between `let`, `const`, and `var` in JavaScript?
The main difference lies in their scoping and hoisting behavior. `let` has block scope, `const` is used for constants, and `var` has function scope and is hoisted.
Example:
```javascript
// Example using let
let x = 10;
// Example using const
const PI = 3.14;
// Example using var
var y = 5;
```
Ques 13. Explain the concept of closures in JavaScript.
Closures allow a function to access variables from its outer (enclosing) scope even after the outer function has finished executing. They help in creating private variables and methods.
Example:
```javascript
function outer() {
let x = 10;
function inner() {
console.log(x);
}
return inner;
}
const closureExample = outer();
closureExample(); // Outputs 10
```
Ques 14. What is the purpose of the 'use strict' directive in JavaScript?
The 'use strict' directive enforces a stricter set of parsing and error handling rules in JavaScript. It helps catch common coding errors and prevents the use of certain error-prone features.
Example:
```javascript
'use strict';
// Strict mode code goes here
```
Ques 15. Explain the box model in CSS.
The box model consists of content, padding, border, and margin. It defines how these components contribute to the overall size of an element.
Example:
```css
.box {
width: 200px;
padding: 20px;
border: 2px solid #000;
margin: 10px;
}```
Most helpful rated by users: