JavaScript Answers
0:000:00
JavaScript Answers
Basic JavaScript Answers
# | Question | Answer | Examples |
---|---|---|---|
1 | What is JavaScript? | A programming language primarily used for web development | Making web pages interactive, handling events, manipulating the DOM |
2 | What is the purpose of JavaScript? | Adds dynamic behavior and interactivity to web pages | Responding to button clicks, updating content without reloading the page |
3 | What is a variable? | A container used to store data | let name = "Alice";, const age = 30; |
4 | How do you declare a variable? | Using let, const, or var | let count = 0;, const PI = 3.14;, var message = "Hello"; |
5 | What is the difference between let and const? | let can be reassigned; const cannot be reassigned | let x = 1; x = 2; (valid); const y = 1; y = 2; (invalid) |
6 | What is a function? | A block of reusable code that performs a specific task | function greet(name) { console.log("Hello, " + name); } |
7 | How do you call a function? | Using the function name followed by parentheses () | greet("Bob"); |
8 | What is an if statement? | Executes a code block if a condition is true | if (age >= 18) { console.log("Adult"); } |
9 | What is an else statement? | Executes a code block if the if condition is false | if (age >= 18) { console.log("Adult"); } else { console.log("Minor"); } |
10 | What is a for loop? | Executes a code block a specific number of times | for (let i = 0; i < 5; i++) { console.log(i); } |
11 | What is a while loop? | Executes a code block as long as a condition is true | let i = 0; while (i < 5) { console.log(i); i++; } |
12 | What is an array? | An ordered list of items | let colors = ["red", "green", "blue"]; |
13 | How do you access an element in an array? | Using its index (starting from 0) | console.log(colors[0]); (outputs "red") |
14 | What is an object? | A collection of key-value pairs (properties and methods) | let person = { name: "Charlie", age: 25 }; |
15 | How do you access a property of an object? | Using dot notation (.) or bracket notation ([]) | console.log(person.name); or console.log(person["age"]); |
Intermediate JavaScript Answers
# | Question | Answer | Examples |
---|---|---|---|
1 | What is the DOM (Document Object Model)? | A tree-like representation of an HTML document | Allows JavaScript to interact with and modify HTML elements |
2 | How do you select an HTML element using JavaScript? | Using methods like document.getElementById(), document.querySelector() | let myElement = document.getElementById("myId"); |
3 | What is document.querySelector()? | Selects the first element matching a specified CSS selector | let paragraph = document.querySelector("p"); |
4 | What is document.querySelectorAll()? | Selects all elements matching a specified CSS selector | let items = document.querySelectorAll(".item"); |
5 | How do you change the text content of an element? | Using the textContent or innerText property | myElement.textContent = "New Text"; |
6 | How do you change the CSS style of an element? | Using the style property | myElement.style.color = "red"; |
7 | What is an event? | An action that occurs in the browser (e.g., click, keypress, mouseover) | click, keydown, mouseover |
8 | How do you attach an event listener to an element? | Using the addEventListener() method | myButton.addEventListener("click", function() { console.log("Button clicked!"); }); |
9 | What is an arrow function? | A concise syntax for writing functions | const add = (a, b) => a + b; |
10 | What is the difference between function and =>? | function has its own this binding; arrow functions inherit this from parent scope | function regular() { console.log(this); }, const arrow = () => { console.log(this); } |
11 | What is the map() method? | Creates a new array by calling a function on every element in the original array | let numbers = [1, 2, 3]; let doubled = numbers.map(n => n * 2); (doubled is [2, 4, 6]) |
12 | What is the filter() method? | Creates a new array with all elements that pass a test implemented by a function | let numbers = [1, 2, 3, 4, 5]; let even = numbers.filter(n => n % 2 === 0); (even is [2, 4]) |
13 | What is the reduce() method? | Executes a reducer function on each element of the array, resulting in a single value | let numbers = [1, 2, 3]; let sum = numbers.reduce((acc, curr) => acc + curr, 0); (sum is 6) |
14 | What is scope? | The region where a variable is accessible | Global scope, function scope, block scope (let, const) |
15 | What is a closure? | A function that retains access to variables from its outer scope even after the outer function has finished | function outer(name) { function inner() { console.log(name); } return inner; } |
Advanced JavaScript Answers
# | Question | Answer | Examples |
---|---|---|---|
1 | Explain the Event Loop. | A mechanism that allows JavaScript to handle asynchronous operations without blocking execution | Call Stack, Callback Queue, Microtask Queue, Event Loop process |
2 | What is asynchronous JavaScript? | JavaScript code that doesn't block the main thread, allowing other tasks to run | setTimeout, setInterval, fetch, Promises, async/await |
3 | What is setTimeout()? | Executes a function after a specified delay (milliseconds) | setTimeout(() => { console.log("Delayed"); }, 2000); |
4 | What is setInterval()? | Executes a function repeatedly at a specified interval | setInterval(() => { console.log("Tick"); }, 1000); |
5 | What is a Promise? | An object representing the eventual completion (or failure) of an asynchronous operation | new Promise((resolve, reject) => { /_ async code _/ }).then(result => ...).catch(error => ...); |
6 | What is async/await? | Syntax sugar over Promises, making asynchronous code look and feel more synchronous | async function fetchData() { const response = await fetch('/api'); ... } |
7 | What is the this keyword? | Refers to the context in which a function is currently executing (depends on how it's called) | obj = { method: function() { console.log(this); } }; obj.method(); (this is obj) |
8 | Explain call(), apply(), and bind(). | Methods to explicitly set the this value for a function | func.call(thisArg, arg1, arg2);, func.apply(thisArg, [arg1, arg2]);, let boundFunc = func.bind(thisArg, arg1, arg2); |
9 | What is a constructor function? | A function used to create and initialize new objects (often used with new) | function Person(name) { this.name = name; } let p1 = new Person("Dave"); |
10 | Explain the concept of prototype inheritance. | JavaScript's inheritance mechanism where objects are linked to each other, sharing properties/methods | Object.prototype, Array.prototype, Person.prototype |
11 | What is a class (ES6)? | Syntax sugar over prototype inheritance, providing a cleaner syntax | class Animal { constructor(name) { this.name = name; } speak() { console.log(this.name); } } |
12 | What is the spread syntax (...)? | Expands an iterable (like an array or string) into individual elements | let arr1 = [1, 2]; let arr2 = [...arr1, 3, 4]; (arr2 is [1, 2, 3, 4]) |
13 | What is the rest parameter (...)? | Collects remaining arguments of a function into an array | function sum(...args) { return args.reduce((a, b) => a + b, 0); } |
14 | What is destructuring assignment? | Extracts values from arrays or properties from objects into distinct variables | let [a, b] = [1, 2];, let { name, age } = { name: "Eve", age: 28 }; |
15 | What is the difference between == and ===? | == performs type coercion; === does not (strict equality) | 1 == "1" (true); 1 === "1" (false) |