Key Concepts in JavaScript

What are the different data types in JavaScript?
What is hoisting in JavaScript?
What is the difference between null and undefined?
What are closures in JavaScript?
What is a callback function in JavaScript?
What are promises in JavaScript?
What is the purpose of the setTimeout() function in Javascript?
How can you check if an array includes a certain value?
How can you remove duplicates in an array?
What is the purpose of async and await in JavaScript?

  Key Concepts in JavaScript 1. Data Types in JavaScript JavaScript has several built-in data types, which can be categorized into two groups: primitive and non-primitive (or reference) types. Primitive Data Types: - String: Represents text. Example: 'Hello World'. - Number: Represents both integer and floating-point numbers. Example: 42, 3.14. - Boolean: Represents a logical entity that can have two values: true or false. - Undefined: A variable that has been declared but not assigned a value is of type undefined. - Null: Represents the intentional absence of any object value, often used to indicate "no value". - Symbol: A unique and immutable primitive value, introduced in ES6, primarily used as object property keys. - BigInt: A numerical type that can represent integers with arbitrary precision, introduced to handle large integers beyond the safe integer limit for Numbers. Non-Primitive Data Types: - Object: A collection of properties, where each property is defined as a key-value pair. Objects can also include functions (methods). 2. Hoisting in JavaScript Hoisting is a behavior in JavaScript where variable and function declarations are moved to the top of their containing scope during the compilation phase. This means that you can use variables and functions before they are declared in the code. For example: console.log(myVar); // undefined var myVar = 5; console.log(myVar); // 5 In the above code, myVar is hoisted to the top, but its assignment does not occur until the line of code is executed. 3. Difference Between Null and Undefined - Undefined: This type indicates that a variable has been declared but has not yet been assigned a value. It is automatically assigned to variables that are declared without an explicit value. Example: let x; console.log(x); // undefined - Null: This is an intentional assignment of a 'no value' state. It represents the absence of any object's value. Example: let y = null; console.log(y); // null 4. Closures in JavaScript A closure is a feature in JavaScript where an inner function retains access to the outer function's variables even after the outer function has completed execution. This is often used for data encapsulation. Example: function outerFunction() { let outerVariable = 'I am outside!'; function innerFunction() { console.log(outerVariable); // Accessing outer function's variable } return innerFunction; } const closureFunction = outerFunction(); closureFunction(); // Output: 'I am outside!' 5. Callback Function in JavaScript A callback function is a function that is passed as an argument to another function and is executed after a certain event or condition is met. It allows for asynchronous operations and enables functions to be executed in a specific order. Example: function fetchData(callback) { // Simulating an asynchronous operation setTimeout(() => { const data = 'Data fetched'; callback(data); }, 1000); } fetchData((result) => { console.log(result); // Output: 'Data fetched' }); 6. Promises in JavaScript A Promise is an object representing the eventual completion (or failure) of an asynchronous operation and its resulting value. Promises have three states: - Pending: The initial state; neither fulfilled nor rejected. - Fulfilled: The operation completed successfully. - Rejected: The operation failed. Example: const myPromise = new Promise((resolve, reject) => { const success = true; // Simulate success or failure if (success) { resolve('Operation successful!'); } else { reject('Operation failed.'); } }); myPromise .then(result => console.log(result)) // Output if successful .catch(error => console.log(error)); // Output if failed 7. Purpose of setTimeout() Function in JavaScript The setTimeout() function is used to execute a specified function after a designated period of time (in milliseconds). It allows for delayed execution of code. Example: setTimeout(() => { console.log('Executed after 2 seconds'); }, 2000); 8. Checking if an Array Includes a Certain Value You can check if an array includes a certain value using the Array.prototype.includes() method, which returns true if the array contains the specified element, and false otherwise. Example: const fruits = ['apple', 'banana', 'orange']; console.log(fruits.includes('banana')); // Output: true console.log(fruits.includes('grape')); // Output: false 9. Removing Duplicates in an Array You can remove duplicates from an array using various methods such as using a Set or the filter() method. Using Set: const numbers = [1, 2, 2, 3, 4, 4]; const uniqueNumbers = [...new Set(numbers)]; console.log(uniqueNumbers); // Output: [1, 2, 3, 4] Using filter(): const numbers = [1, 2, 2, 3, 4, 4]; const uniqueNumbers = numbers.filter((value, index, self) => self.indexOf(value) === index); console.log(uniqueNumbers); // Output: [1, 2, 3, 4] 10. Purpose of async and await in JavaScript The async and await keywords are used to handle asynchronous operations more conveniently and readably than traditional promise chaining. - async: When applied to a function, it makes the function return a promise. If the function returns a value, that value is wrapped in a resolved promise; if it throws an error, it returns a rejected promise. - await: This keyword can only be used inside async functions and pauses the execution of the async function until the promise is resolved or rejected. Example: async function fetchData() { try { const response = await fetch('https://api.example.com/data'); const data = await response.json(); console.log(data); } catch (error) { console.error('Error fetching data:', error); } } fetchData(); In this example, fetchData waits for the fetch request to complete before continuing to process the response.

Sample Answer