1.What is JavaScript?
JavaScript is a scripting language used to create interactive websites, web applications, and web servers. It's a high-level, interpreted programming language that's used to add functionality and interactivity to web pages.
2.What are the differences between JavaScript and Java?
3.What are the different data types in JavaScript?
JavaScript has six primitive data types: undefined, null, boolean, number, string, and symbol. It also has one complex data type, Object.
4.What is the difference between null and undefined in JavaScript?
Undefined means a variable has been declared but has not yet been assigned a value. Null is an assignment value that represents no value or no object.
Example
var a;
console.log(a); // undefined
var b = null;
console.log(b); // null
5.What is hoisting in JavaScript?
Hoisting is a JavaScript mechanism where variables and function declarations are moved to the top of their respective scopes. This means that you can use a variable or function before it has been declared.
Example:
console.log(a); // undefined
var a = 1;
6.What is closure in JavaScript?
A closure is a function that has access to variables in its outer (enclosing) function scope, even after the outer function has returned.
Example
function outer() {
var a = 1;
function inner() {
console.log(a);
}
return inner;
}
var fn = outer();
fn(); // 1
7.What is the use of the "this" keyword in JavaScript?
The "this" keyword refers to the object that the function is a method of. In other words, it refers to the object that the function belongs to.
Example:
const myObj = {
name: "John",
sayName() {
console.log(`My name is ${this.name}`);
}
};
myObj.sayName(); // output: "My name is John"
8.What is event bubbling in JavaScript?
Event bubbling is the propagation of an event from the innermost element to the outermost element.
Example:
<div>
<p>Hello</p>
</div>
document.querySelector("p").addEventListener("click", function(event) {
console.log("Hello");
});
// If the user clicks the <p> element, the output will be:
// "Hello"
9.What is the difference between the "==" and "===" operators in JavaScript?
The "==" operator compares values, while the "===" operator compares values and data types.
Example:
console.log(5 == "5"); // output: true
console.log(5 === "5"); // output: false
10.What is the difference between "let" and "var" in JavaScript?
The main difference between "let" and "var" is that "let" is block-scoped, while "var" is function-scoped.
Example:
function example() {
var a = 1;
if (true) {
var b = 2;
let c = 3;
}
console.log(a); // 1
console.log(b); // 2
console.log(c); // ReferenceError: c is not defined
}
11.What is the difference between an array and an object in JavaScript?
An array is an ordered list of values, while an object is an unordered collection of key-value pairs.
12.What is the "new" keyword in JavaScript used for?
The "new" keyword is used to create an instance of a user-defined object or of one of the built-in object types.
Example:
function Person(name, age) {
this.name = name;
this.age = age;
}
const john = new Person("John", 30);
console.log(john); // output: { name: "John", age: 30 }
13.What is the difference between synchronous and asynchronous code in JavaScript?
Synchronous code executes in a single thread and blocks the execution of subsequent code until it is complete. Asynchronous code allows subsequent code to execute immediately while it waits for some operation to complete, such as an HTTP request or a user interaction.
Example:
// Synchronous code
console.log("Step 1");
console.log("Step 2");
console.log("Step 3");
// Asynchronous code
console.log("Step 1");
setTimeout(function() {
console.log("Step 2");
}, 1000);
console.log("Step 3");
14.What is the difference between a function declaration and a function expression in JavaScript?
A function declaration is a named function that is defined at the top-level of a script or function body. A function expression is an anonymous function that is assigned to a variable.
Example:
// Function declaration
function example() {
console.log("Hello, world!");
}
// Function expression
var example = function() {
console.log("Hello, world!");
};
15.What is the use of the "use strict" directive in JavaScript?
The "use strict" directive enables strict mode, which makes it easier to write secure and optimized JavaScript code. It enables more rigorous parsing and error handling, and also disallows certain unsafe or deprecated features.
Example:
"use strict";
// Code that follows is in strict mode
16.What is a higher-order function in JavaScript?
A higher-order function is a function that takes one or more functions as arguments or returns a function as its result.
Example:
function double(x) {
return x * 2;
}
function triple(x) {
return x * 3;
}
function apply(func, x) {
return func(x);
}
console.log(apply(double, 5)); // Output: 10
console.log(apply(triple, 5)); // Output: 15
17.What is the difference between a for loop and a for...in loop in JavaScript?
A for loop is used to iterate over an array, whereas a for...in loop is used to iterate over the properties of an object.
Example:
let arr = [1, 2, 3];
for (let i = 0; i < arr.length; i++) {
console.log(arr[i]);
}
let obj = {a: 1, b: 2, c: 3};
for (let prop in obj) {
console.log(obj[prop]);
}
18.What is a prototype in JavaScript?
A prototype is an object that is associated with every function and provides a blueprint for creating new objects.
19.What is an IIFE in JavaScript?
An IIFE (Immediately Invoked Function Expression) is a function that is immediately executed after it is defined.
Example:
(function() {
console.log('Hello, World!');
})();
20.What is a callback function in JavaScript?
A callback function is a function that is passed as an argument to another function and is called after the completion of some task.
Example:
function doSomething(callback) {
console.log('Doing something...');
callback();
}
doSomething(function() {
console.log('Done!');
});
21.What is a promise in JavaScript?
A promise is an object that represents the eventual completion (or failure) of an asynchronous operation and its resulting value.
Example:
function getData() {
return new Promise(function(resolve, reject) {
fetch('https://example.com/data')
.then(response => response.json())
.then(data => resolve(data))
.catch(error => reject(error));
});
}
getData()
.then(function(data) {
console.log(data);
})
.catch(function(error) {
console.error(error);
});
22.What is the difference between const and let in JavaScript?
The const keyword is used to declare a read-only named constant, whereas the let keyword is used to declare a block-scoped variable.
Example:
const PI = 3.14;
PI = 3.14159; // TypeError: Assignment to constant variable.
let x = 10;
x = 20;
console.log(x); // Output: 2
23.What is the difference between a map and a set in JavaScript?
A map is an object that maps keys to values, whereas a set is a collection of unique values.
Example:
// Map
let map = new Map();
map.set('a', 1);
map.set('b', 2);
console.log(map.get('a')); // Output: 1
// Set
let set = new Set();
set.add(1);
set.add(2);
set.add(1);
console.log(set.size); // Output: 2
24.What is the difference between a spread operator and a rest parameter in JavaScript?
A spread operator is used to spread an array or an object into individual elements, whereas a rest parameter is used to collect multiple arguments into an array.
Example:
// Spread operator
let arr1 = [1, 2, 3];
let arr2 = [...arr1, 4, 5, 6];
console.log(arr2); // Output: [1, 2, 3, 4, 5, 6]
// Rest parameter
function sum(...numbers) {
return numbers.reduce((acc, curr) => acc + curr);
}
console.log(sum(1, 2, 3, 4, 5)); // Output: 15
25.What is destructuring in JavaScript?
Destructuring is a way to extract values from objects or arrays into separate variables.
Example:
// Object destructuring
let obj = {a: 1, b: 2};
let {a, b} = obj;
console.log(a, b); // Output: 1 2
// Array destructuring
let arr = [1, 2, 3];
let [x, y, z] = arr;
console.log(x, y, z); // Output: 1 2 3
26.What is event capturing in JavaScript?
Event capturing is a way in which events propagate down the DOM tree, from the outermost element to the innermost element.
Example:
<div id="outer">
<div id="inner">
<button id="button">Click me</button>
</div>
</div>
<script>
let outer = document.getElementById('outer');
let button = document.getElementById('button');
button.addEventListener('click', function() {
console.log('Button clicked');
}, true);
outer.addEventListener('click', function() {
console.log('Outer clicked');
}, true);
</script>
27.What is event delegation in JavaScript?
Event delegation is a way in which a parent element handles events for its child elements.
Example:
<ul id="list">
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
</ul>
<script>
let list = document.getElementById('list');
list.addEventListener('click', function(event) {
if (event.target.tagName === 'LI') {
console.log('Item clicked:', event.target.textContent);
}
});
</script>
When a list item is clicked, the event bubbles up to the parent ul element, which handles the event and checks if the clicked element is an li element.
28.What is a JavaScript event?
A JavaScript event is an action that occurs on a web page, such as a user clicking a button or scrolling the page, that can be detected by the browser and used to trigger JavaScript code.
Example
<button onclick="console.log('Button clicked')">Click me</button>
29.What is the difference between a callback and a promise in JavaScript?
A callback is a function that is passed as an argument to another function and is executed after some event occurs, while a promise is an object that represents the eventual completion or failure of an asynchronous operation and allows you to chain asynchronous operations together.
Example (callback):
function fetchData(callback) {
setTimeout(function() {
callback('Data');
}, 1000);
}
fetchData(function(data) {
console.log(data); // Output: Data (after 1 second)
});
Example (promise):
function fetchData() {
return new Promise(function(resolve, reject) {
setTimeout(function() {
resolve('Data');
}, 1000);
});
}
fetchData().then(function(data) {
console.log(data); // Output: Data (after 1 second)
});
30.What is the difference between an object and an array in JavaScript?
An object is a collection of key-value pairs, while an array is an ordered collection of values.
Example:
// Object
let obj = {a: 1, b: 2, c: 3};
console.log(obj.a); // Output: 1
// Array
let arr = [1, 2, 3];
console.log(arr[0]); // Output: 1
0 Comments
Please don't send any spam Link.