JS Programming Answers


0:00
0:00

50 Simple JavaScript Programming Answers

#QuestionAnswer
1How do you declare a variable?var, let, or const
2What is the difference between let and var?let is block-scoped, can be reassigned; var is function-scoped, can be reassigned
3What is the difference between const and let?const is block-scoped, cannot be reassigned; let is block-scoped, can be reassigned
4How do you create an array?const arr = [1, 2, 3];
5How do you create an object?const obj = { name: 'Alice', age: 30 };
6How do you access an element in an array?arr[0]
7How do you access a property in an object?obj.name or obj['name']
8How do you define a function?function myFunction() { ... } or const myFunction = () => { ... };
9How do you call a function?myFunction();
10What is an if-else statement?if (condition) { ... } else { ... }
11What is a for loop?for (let i = 0; i < 5; i++) { ... }
12What is a while loop?while (condition) { ... }
13How do you add an element to an array?arr.push(element);
14How do you remove an element from an array?arr.pop(); (last), arr.shift(); (first), arr.splice(index, 1);
15How do you iterate over an array?arr.forEach(item => { ... }); or for (const item of arr) { ... }
#QuestionAnswerExample
16What is the map() method?Creates a new array by calling a function on every element in the original arrayconst newArr = arr.map(x => x * 2);
17What is the filter() method?Creates a new array with all elements that pass a testconst filteredArr = arr.filter(x => x > 5);
18What is the reduce() method?Executes a reducer function on each element of the array, resulting in a single valueconst sum = arr.reduce((acc, curr) => acc + curr, 0);
19What is an arrow function?A concise syntax for writing functionsconst add = (a, b) => a + b;
20What is the difference between == and ===?== is loose equality (type coercion); === is strict equality (no type coercion)5 == '5' (true), 5 === '5' (false)
21What is undefined?A variable has been declared but not yet assigned a valuelet x; console.log(x); // undefined
22What is null?Represents the intentional absence of any object valuelet y = null; (explicitly set to null)
23What is a Promise?An object representing the eventual completion (or failure) of an asynchronous operationnew Promise((resolve, reject) => { ... });
24What is async/await?Syntax sugar over Promises, making asynchronous code look and feel more synchronousasync function fetchData() { const response = await fetch('/api'); ... }
25What is a try...catch block?Used for error handling in synchronous codetry { ... } catch (error) { ... }
26What is a Closure?A function that has access to variables from its outer scope, even after the outer function has finishedfunction outer() { let x = 10; function inner() { console.log(x); } return inner; }
27What is the this keyword?Refers to the object that owns the currently executing code (context depends on how it's called)const obj = { method: function() { console.log(this); } };
28What is the DOM (Document Object Model)?A tree-like structure representing HTML and XML documentsdocument.getElementById('myId');
29How do you select an HTML element?Using document.getElementById(), document.querySelector(), document.querySelectorAll()document.querySelector('.myClass');
30How do you add an event listener?Using element.addEventListener()button.addEventListener('click', () => { ... });
31What is JSON (JavaScript Object Notation)?A lightweight data-interchange format{ "name": "Bob", "age": 25 }
32How do you convert a JS object to a JSON string?JSON.stringify(obj)const jsonString = JSON.stringify(obj);
33How do you convert a JSON string to a JS object?JSON.parse(jsonString)const obj = JSON.parse(jsonString);
34What are Template Literals?String literals using backticks (`) allowing embedded expressionsconst message = Hello, ${name}!;
35What is Destructuring Assignment?Extracting values from arrays or properties from objects into distinct variablesconst { name, age } = { name: 'Alice', age: 30 }; or const [a, b] = [1, 2];
36What is the Spread Syntax (...)?Expands an iterable (like an array or string) into individual elementsconst newArr = [...arr1, ...arr2];, const newObj = { ...obj1, c: 3 };
37What are Rest Parameters (...)?Collects an indefinite number of arguments into an arrayfunction sum(...args) { return args.reduce((a, b) => a + b, 0); }
38What is a Callback Function?A function passed as an argument to another function to be executed latersetTimeout(callback, 1000);
39What is Callback Hell?Nested callbacks that make code difficult to read and maintainPromises offer a cleaner alternative.
40What is the purpose of super?Calls the constructor or methods of the parent classclass Child extends Parent { constructor() { super(); ... } }
41What is the instanceof operator?Checks if an object is an instance of a specific class or constructor functionif (obj instanceof Array) { ... }
42What is the in operator?Checks if a property exists in an object (either directly or in its prototype chain)if ('name' in obj) { ... }
43What is setTimeout()?Executes a function after a specified delaysetTimeout(callback, 1000);
44What is setInterval()?Executes a function repeatedly at a specified time intervalsetInterval(callback, 1000);
45What is clearTimeout()?Stops a timer created with setTimeout()clearTimeout(timerId);
46What is clearInterval()?Stops a timer created with setInterval()clearInterval(intervalId);
47What is the difference between call(), apply(), and bind()?All set the this value for a function. call takes arguments individually; apply takes arguments as an array; bind returns a new function with fixed this.func.call(thisArg, arg1, arg2); func.apply(thisArg, [arg1, arg2]); const boundFunc = func.bind(thisArg);
48What is a Shallow Copy?Creates a new object/array with references to the same nested objects/arrays as the originalconst newObj = { ...obj }; or const newArr = [...arr];
49What is a Deep Copy?Creates a completely new object/array, including copies of all nested objects/arraysJSON.parse(JSON.stringify(obj)) (simple, but has limitations) or libraries like _.cloneDeep
50What is the typeof operator?Returns a string indicating the type of the operandtypeof 'hello' ('string'), typeof 10 ('number'), typeof {} ('object'), typeof [] ('object'), typeof function(){} ('function')

Last updated on July 28, 2025

🔍 Explore More Topics

Discover related content that might interest you

TwoAnswers Logo

Providing innovative solutions and exceptional experiences. Building the future.

© 2025 TwoAnswers.com. All rights reserved.

Made with by the TwoAnswers.com team