Skip to main content

Top 50 JavaScript Interview Questions & Answers (Beginner to Advanced)

Whether you're preparing for a web development job or improving your JS skills, these 50 JavaScript interview questions will help you master the important concepts.


1. What is JavaScript?

JavaScript is a high-level programming language used to make web pages interactive.


2. How do you print output in JavaScript?

document.write("Hello World!");
console.log("Printed successfully");

3. What are variables?

Variables store data. JS has var, let, and const.

let name = "Alex";
const age = 21;
document.write(name + " - " + age);

4. Difference between let and var?

  • var → function scoped
  • let → block scoped (recommended)

5. What are data types in JavaScript?

  • String
  • Number
  • Boolean
  • Undefined
  • Null
  • Object
  • Array

6. What is typeof?

document.write(typeof "Hello");

7. What is a function?

A function is a reusable block of code.

function greet(){
  document.write("Welcome!");
}
greet();

8. What is an arrow function?

const add = (a, b) => a + b;
document.write(add(2,3));

9. What is an object?

let user = { name: "John", age: 22 };
document.write(user.name);

10. What is an array?

let skills = ["HTML", "CSS", "JS"];
document.write(skills.join(", "));

11. What is DOM?

DOM (Document Object Model) allows JavaScript to control and modify HTML.


12. Get element by ID example

document.write("<h2 id='title'>Old Text</h2>");
document.getElementById("title").innerHTML = "Changed using JS!";

13. What is an event?

Event = something user does (click, hover, keypress, etc.).


14. Example of click event

document.write('<button id="btn">Click Me</button>');
document.getElementById("btn").onclick = () => {
  alert("Button Clicked!");
};

15. What is localStorage?

It stores small data in the browser, even after refresh.


16. Save to localStorage

localStorage.setItem("user", "Alex");
document.write("Saved!");

17. Get from localStorage

document.write(localStorage.getItem("user"));

18. What is sessionStorage?

Similar to localStorage, but removed when the tab is closed.


19. What is JSON?

JSON = JavaScript Object Notation (data format).


20. Convert object to JSON

let user = { name: "Alex" };
document.write(JSON.stringify(user));

21. What is a promise?

Promise handles async operations.


22. Example Promise

let p = new Promise(res => res("Done!"));
p.then(msg => document.write(msg));

23. What is async/await?

Cleaner way to handle promises.


24. Example async/await

async function test(){
  return "Hello Async!";
}
test().then(msg => document.write(msg));

25. What is NaN?

Not-a-Number (invalid number result).


26. What is isNaN()?

``` isNaN("ABC") // true ```

27. What is hoisting?

JS moves variable and function declarations to the top before execution.


28. What are template literals?

let name = "Alex";
document.write(`Hello, ${name}`);

29. What is == vs ===?

  • == compares value
  • === compares value + type

30. What is a callback?

A function passed as argument to another function.


31. Example callback

function greet(cb){
  cb();
}
greet(() => document.write("Callback called!"));

32. What is setTimeout?

setTimeout(() => document.write("Hello after 1 second"), 1000);

33. What is setInterval?

let i = 1;
setInterval(() => document.write(i++ + " "), 500);

34. What is a class?

class User{
  constructor(name){
    this.name = name;
  }
}
let u = new User("Alex");
document.write(u.name);

35. What is inheritance?

``` class Child extends Parent {} ```

36. What is spread operator?

``` let arr = [1,2,3]; let newArr = [...arr]; ```

37. What is rest operator?

``` function sum(...nums){ return nums.reduce((a,b) => a + b); } ```

38. What is map()?

``` [1,2,3].map(x => x * 2) ```

39. What is filter()?

``` [1,2,3,4].filter(x => x % 2 === 0) ```

40. What is reduce()?

``` [1,2,3].reduce((a,b) => a + b) ```

41. What is try/catch?

``` try { ... } catch(err){ ... } ```

42. What is strict mode?

``` "use strict"; ```

43. What is AJAX?

AJAX loads data from server without page refresh.


44. What is fetch()?

``` fetch(url).then(...) ```

45. What is a closure?

Function remembering its outer scope.


46. Example closure

``` function outer(){ let x = 5; return function(){ return x; } } ```

47. What is event bubbling?

Event travels from child → parent DOM nodes.


48. What is event capturing?

Event travels from parent → child.


49. What is debounce?

Function runs after user stops triggering event.


50. What is throttle?

Limits how often function executes over time.


Final Words

These 50 JavaScript questions will help you prepare for real frontend/web developer interviews, create better JS logic, and improve coding confidence.

Comments

Popular posts from this blog

HTML Tables Tutorial — Learn Tables with Examples Tables organize tabular data (rows & columns). They are great for schedules, price lists, spec sheets, comparison charts, and any data that fits a grid. This guide covers: Table basics: <table> , <tr> , <td> , <th> Headers, captions, and groups ( <caption> , <thead> , <tbody> , <tfoot> ) Colspan & rowspan Styling and responsive tables Accessibility and best practices Table of Contents Basic Structure Headers & Captions Colspan & Rowspan Grouping Rows: thead / tbody / tfoot Styling Tables Responsive Tables Accessibility Tips Practical Examples Best Practices 1. Basic Structure Minimal valid table: <table> <tr> <td>Cell 1</td> <td>Cell 2</td> </tr> </table> Elements: <table> — container for the table <tr> — table ...
HTML Forms Tutorial HTML Forms Tutorial HTML Forms allow users to enter and submit data. They are used everywhere—login forms, signup forms, search boxes, feedback forms, etc. In this complete tutorial, you will learn all form elements with examples and demos. 1. What is an HTML Form? A form collects user input using different form elements like text fields, checkboxes, radio buttons, buttons, and more. <form> Form elements go here... </form> 2. Basic Form Structure <form action="#" method="post"> <input type="text" placeholder="Enter name"> <button>Submit</button> </form> Submit Attributes: action → URL where form data is sent method="POST" → Secure data sending method="GET" → Data shows in URL 3. Text Input Field <input type="text" placeholder="Your Name...
What is HTML? A Beginner-Friendly Explanation If you want to become a web developer, HTML is the first language you should learn. It is the foundation of every website on the internet. In simple words: HTML (HyperText Markup Language) is used to create the structure and content of a webpage. Why HTML is important? Websites cannot exist without HTML It is simple and beginner-friendly Used with CSS and JavaScript Works on all browsers How does HTML work? HTML is made of tags . Tags tell the browser what type of content is being shown. Example of an HTML tag: <p>This is a paragraph.</p> Here: <p> is the opening tag </p> is the closing tag The text inside is the content Basic HTML Page Example Below is a simple webpage example you can try: <!DOCTYPE html> <html> <head> <title>My First Webpage</title> </head> <body> <h1>Hello World!</h1> <p>This is my...