Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
14 views

JavaScript Viva

This document contains 36 questions about JavaScript concepts along with their detailed answers to help prepare for an interview or assessment. The questions cover topics like data types, scope, hoisting, closures, prototypes, promises, async/await, the event loop, and more.

Uploaded by

karthik thevar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

JavaScript Viva

This document contains 36 questions about JavaScript concepts along with their detailed answers to help prepare for an interview or assessment. The questions cover topics like data types, scope, hoisting, closures, prototypes, promises, async/await, the event loop, and more.

Uploaded by

karthik thevar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

JavaScript viva (oral exam) questions along with their answers to help you prepare for

an interview or assessment:

1. **What is JavaScript, and how is it different from Java?**


- **Answer:** JavaScript is a lightweight, interpreted, or JIT-compiled programming language
with first-class functions. While both Java and JavaScript are used for web development, Java is
a statically typed, object-oriented programming language that runs on a virtual machine or
browser, while JavaScript is dynamically typed and runs in the browser's environment.

2. **Explain the difference between `var`, `let`, and `const`.**


- **Answer:** `var` is function-scoped and can be redeclared; `let` is block-scoped and cannot
be redeclared in the same scope; `const` is block-scoped, cannot be redeclared, and must be
initialized at the time of declaration.

3. **What are JavaScript data types? List them.**


- **Answer:** The primary data types in JavaScript are: `Number`, `String`, `Boolean`,
`Object`, `Undefined`, `Null`, `Symbol`, and `BigInt`.

4. **Explain hoisting in JavaScript.**


- **Answer:** Hoisting is JavaScript's default behavior of moving declarations to the top of the
current scope (script or function). This means variables and function declarations can be used
before they are declared.

5. **What are closures in JavaScript?**


- **Answer:** A closure is a function that retains access to its lexical scope even when the
function is executed outside that scope. This allows for data encapsulation.

6. **Explain the concept of scope in JavaScript.**


- **Answer:** Scope determines the accessibility (visibility) of variables. JavaScript has
function scope, block scope, and global scope.

7. **What is the difference between `==` and `===`?**


- **Answer:** `==` checks for value equality after type conversion, while `===` checks for strict
equality without type conversion.

8. **How does prototypal inheritance work in JavaScript?**


- **Answer:** Objects in JavaScript can inherit properties and methods from another object,
referred to as the prototype. Each object has a `__proto__` property that points to its prototype.

9. **What are Immediately Invoked Function Expressions (IIFE)?**


- **Answer:** IIFE is a function that is executed immediately after it is defined. The syntax is
`(function() { /* code */ })();`.

10. **Explain the use of the `this` keyword in JavaScript.**


- **Answer:** `this` refers to the object it belongs to. Its value depends on the context in which
a function is called: in a method, it refers to the owner object; in a function, it refers to the global
object (or `undefined` in strict mode).

11. **What is an arrow function, and how is it different from a regular function?**
- **Answer:** Arrow functions are a shorter syntax for writing functions. They do not have their
own `this`, arguments, super, or new.target bindings, making them unsuitable for methods.

12. **What are promises, and how do you use them?**


- **Answer:** Promises represent the eventual completion (or failure) of an asynchronous
operation. They are used to handle asynchronous operations by chaining `.then` and `.catch`
methods.

13. **Explain async/await in JavaScript.**


- **Answer:** `async/await` is syntactic sugar built on top of promises, making asynchronous
code look synchronous. `async` functions return a promise, and `await` pauses the execution of
the `async` function until the promise is resolved.

14. **What is event delegation?**


- **Answer:** Event delegation is a technique for handling events efficiently. Instead of adding
an event listener to each child element, a single event listener is added to a parent element, and
events are handled based on the target.

15. **What is the Document Object Model (DOM)?**


- **Answer:** The DOM is a programming interface for HTML and XML documents. It
represents the page so that programs can change the document structure, style, and content.

16. **How do you manipulate the DOM using JavaScript?**


- **Answer:** You can manipulate the DOM using methods like `getElementById`,
`getElementsByClassName`, `querySelector`, `querySelectorAll`, `appendChild`, `removeChild`,
`createElement`, `innerHTML`, `textContent`, and `setAttribute`.

17. **What are events in JavaScript? List some common events.**


- **Answer:** Events are actions or occurrences that happen in the system you are
programming, which the system tells you about so your code can react to them. Common
events include `click`, `load`, `mouseover`, `mouseout`, `keyup`, `keydown`, and `submit`.

18. **Explain the concept of event bubbling and event capturing.**


- **Answer:** Event bubbling is when an event starts from the target element and bubbles up
to the root. Event capturing is the reverse, where the event starts from the root and goes down
to the target element. Event listeners can be set to capture during the capturing phase or the
bubbling phase.

19. **How can you prevent event bubbling?**


- **Answer:** You can prevent event bubbling by using `event.stopPropagation()` in the event
handler.

20. **What is the difference between `null` and `undefined`?**


- **Answer:** `undefined` means a variable has been declared but not yet assigned a value.
`null` is an assignment value that represents no value or no object.

21. **What is the purpose of the `array.map()` method?**


- **Answer:** `array.map()` creates a new array populated with the results of calling a
provided function on every element in the calling array.

22. **What is the purpose of the `array.filter()` method?**


- **Answer:** `array.filter()` creates a new array with all elements that pass the test
implemented by the provided function.

23. **What is the purpose of the `array.reduce()` method?**


- **Answer:** `array.reduce()` executes a reducer function on each element of the array,
resulting in a single output value.

24. **What is the difference between `call()`, `apply()`, and `bind()`?**


- **Answer:** `call()` and `apply()` invoke a function with a specified `this` value and
arguments. `call()` accepts an argument list, while `apply()` accepts a single array of arguments.
`bind()` returns a new function with a specified `this` value and arguments.

25. **What is the event loop in JavaScript?**


- **Answer:** The event loop is a mechanism that handles the execution of multiple chunks of
your program over time, such as executing callbacks, handling events, and running tasks.

26. **What is the difference between synchronous and asynchronous code in JavaScript?**
- **Answer:** Synchronous code is executed in sequence, blocking the execution of
subsequent code until the current task is completed. Asynchronous code allows multiple tasks
to be executed concurrently, with tasks being delegated to the browser or Node.js to handle and
continue processing other tasks.

27. **What is a higher-order function?**


- **Answer:** A higher-order function is a function that can take other functions as arguments
or return a function as a result.

28. **What is the purpose of the `array.forEach()` method?**


- **Answer:** `array.forEach()` executes a provided function once for each array element.

29. **What is the difference between mutable and immutable objects?**


- **Answer:** Mutable objects can be changed after their creation, while immutable objects
cannot.
30. **Explain the concept of "callback hell" and how to avoid it.**
- **Answer:** "Callback hell" refers to the situation where callbacks are nested within other
callbacks, leading to difficult-to-read and maintain code. It can be avoided using promises,
async/await, or modularizing code.

31. **What are generators in JavaScript?**


- **Answer:** Generators are functions that can be paused and resumed, allowing for the
generation of sequences of values. They are defined using the `function*` syntax and use the
`yield` keyword.

32. **What is the difference between `Object.create()` and using a constructor function for
creating objects?**
- **Answer:** `Object.create()` creates a new object with the specified prototype object and
properties, while a constructor function is used to create an instance of an object with `new`.

33. **What are template literals in JavaScript?**


- **Answer:** Template literals are string literals allowing embedded expressions, multi-line
strings, and string interpolation using backticks (`` ` ``) and `${}` for placeholders.

34. **What is the difference between function declarations and function expressions?**
- **Answer:** Function declarations are hoisted and can be used before they are defined.
Function expressions are not hoisted and can only be used after they are defined.

35. **Explain the concept of "strict mode" in JavaScript.**


- **Answer:** Strict mode is a way to opt into a restricted variant of JavaScript, introduced in
ES5. It helps catch common coding mistakes and "unsafe" actions, like defining global
variables.

36. **What are modules in JavaScript?**


- **Answer:** Modules are reusable pieces of code that can be exported from one file and
imported into another. They help in organizing and maintaining code. Common module systems
are ES6 modules

You might also like