JavaScript Interview Questions And Answer Built-in JavaScript Methods – Part 4. Explore essential JavaScript topics including built-in methods, data structures (Map, Set), JSON, local storage, and ES6 modules. A must-read guide for frontend and full-stack developer interviews.


🧰 JavaScript Interview Questions And Answer Built-in JavaScript Methods

46. What are some commonly used Array methods in JavaScript?

  • map(): Creates a new array by transforming elements.
  • filter(): Filters elements based on a condition.
  • reduce(): Reduces array to a single value.
  • forEach(): Executes a function on each element.
const nums = [1, 2, 3, 4];
const doubled = nums.map(n => n * 2); // [2, 4, 6, 8]

47. What are important String methods in JavaScript?

  • charAt()
  • includes()
  • indexOf()
  • slice()
  • split()
  • replace()
  • toUpperCase(), toLowerCase()
const str = "JavaScript";
console.log(str.includes("Script")); // true

48. What are Object utility methods?

  • Object.keys()
  • Object.values()
  • Object.entries()
  • Object.assign()
  • Object.freeze()
const obj = { a: 1, b: 2 };
console.log(Object.entries(obj)); // [['a', 1], ['b', 2]]

🧠 JavaScript Data Structures

49. What is a Map in JavaScript?

Answer:
Map holds key-value pairs and remembers the original insertion order of the keys. Keys can be of any type.

const map = new Map();
map.set("name", "Alice");
map.set(123, "ID");

50. What is a Set in JavaScript?

Answer:
Set is a collection of unique values. Duplicate values are automatically removed.

const set = new Set([1, 2, 2, 3]);
console.log(set); // Set { 1, 2, 3 }

51. Difference between Map and Object?

FeatureMapObject
Key TypesAny (object, primitive)Only string or symbol
OrderOrderedUnordered
PerformanceBetter for frequent additions/removalsGeneral usage

52. What is the difference between Set and Array?

FeatureSetArray
DuplicatesNoYes
OrderYesYes
AccessNo index-based accessIndex-based access

📦 JSON and Local Storage

53. What is JSON in JavaScript?

Answer:
JSON (JavaScript Object Notation) is a format to store and exchange data.

const obj = { name: "John", age: 30 };
const jsonStr = JSON.stringify(obj);
const parsed = JSON.parse(jsonStr);

54. What is localStorage in JavaScript?

Answer:
localStorage allows storing key-value pairs in the browser with no expiration.

localStorage.setItem("name", "Alice");
console.log(localStorage.getItem("name")); // Alice

55. Difference between localStorage and sessionStorage?

FeaturelocalStoragesessionStorage
ExpirationNever (manual removal)When tab is closed
ScopeEntire originPer tab/session

📂 JavaScript Modules

56. What are ES6 modules in JavaScript?

Answer:
ES6 modules allow you to break up your code into separate files.

// utils.js
export function add(a, b) {
  return a + b;
}

// main.js
import { add } from './utils.js';

57. What is the difference between export default and named export?

// Default export
export default function greet() {}

// Named export
export const sayHi = () => {};
  • Default export: Only one per file.
  • Named export: Multiple per file.

58. Can you use CommonJS and ES6 modules together?

Answer:
Not directly. CommonJS (require) is used in Node.js; ES6 modules (import) in modern browsers. Tools like Babel or Webpack can transpile between them.


🔧 Best Practices and Coding Standards

59. What are some JavaScript best practices?

  • Use let and const instead of var.
  • Avoid polluting global scope.
  • Use arrow functions for callbacks.
  • Use descriptive variable names.
  • Write pure functions when possible.
  • Avoid deep nesting.
  • Use === instead of ==.

60. Why should you avoid global variables in JavaScript?

Answer:
Global variables can lead to naming collisions and hard-to-track bugs, especially in larger apps.


61. What is the use of strict mode in JavaScript?

"use strict";

function test() {
  x = 10; // ReferenceError in strict mode
}

It prevents the use of undeclared variables and other unsafe practices.


JavaScript Interview Questions And Answer Built-in JavaScript Methods – Part 4

🧠 Real-World Coding Questions

62. Reverse a string in JavaScript.

function reverse(str) {
  return str.split("").reverse().join("");
}

63. Check if a string is a palindrome.

function isPalindrome(str) {
  return str === str.split("").reverse().join("");
}

64. Find the maximum number in an array.

const arr = [5, 1, 9];
console.log(Math.max(...arr));

65. Flatten a nested array.

const nested = [1, [2, [3, [4]]]];
console.log(nested.flat(Infinity)); // [1, 2, 3, 4]

JavaScript OOP Interview Questions Master Object-Oriented Programming, Functional Programming, DOM, and Event Handling part 3

✅ Up Next: Part 5

In the final part (Part 5), we’ll cover:

  • Asynchronous JavaScript (Promises, async/await, callbacks)
  • Event loop and microtask queue
  • JavaScript memory management
  • JavaScript security practices
  • JavaScript interview tips and preparation checklist
Share.

Welcome to Dastgeertech Studio! We are a dynamic and innovative tech company based in Lahore, Pakistan. At Dastgeertech Studio, we are dedicated to providing cutting-edge technology solutions tailored to meet the unique needs of our clients.

Leave a ReplyCancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Discover more from DastgeerTech Studio

Subscribe now to keep reading and get access to the full archive.

Continue reading

Exit mobile version