DEV Community

Cover image for JavaScript Interview Questions
S M AL RABBI
S M AL RABBI

Posted on

JavaScript Interview Questions

  1. Why do we call JavaScript as dynamic language?
    JavaScript is considered a dynamic language because it allows for dynamic typing, where variables can hold values of any data type without explicit declaration, and the types of variables can change during execution.

  2. How does JavaScript determine data types?
    JavaScript determines data types dynamically, at runtime, based on the type of value assigned to a variable. It automatically converts between types as needed.

  3. What is typeof function?
    typeof is a built-in JavaScript operator that returns the data type of its operand.

  4. How to check data type in JavaScript?
    You can use the typeof operator to check the data type of a value, or you can use other methods like instanceof, Object.prototype.toString.call(), or checking constructors.

  5. What are different data types in JavaScript?
    JavaScript has several primitive data types: number, string, boolean, undefined, null, and symbol. Additionally, there is the object data type which includes arrays, functions, and objects.

  6. Explain Undefined Data types?
    In JavaScript, undefined represents a variable that has been declared but not assigned a value. It is also the default value for uninitialized variables.

  7. What is Null?
    null in JavaScript represents the intentional absence of any object value. It's often used to signify that a variable has no value or that an object does not exist.

  8. Differentiate between Null and Undefined?
    null is explicitly assigned by the programmer to indicate the absence of value, while undefined usually means a variable has been declared but not assigned a value.

  9. Explain Hoisting?
    Hoisting is a JavaScript mechanism where variables and function declarations are moved to the top of their containing scope during the compile phase before code execution.

  10. Are JavaScript initializations hoisted?
    In JavaScript, only variable declarations (not initializations) are hoisted to the top of their containing scope.

Certainly! Let's continue with the remaining questions:

  1. What are global variables?
    Global variables are variables declared outside of any function or block scope. They can be accessed from any part of the program.

  2. What are the issues with Global variables?
    Global variables can lead to naming conflicts, make code harder to understand and maintain, and increase the risk of unintended side effects.

  3. What happens when you declare a variable without VAR?
    When a variable is declared without var, let, or const keywords, it becomes a global variable if it's declared in the global scope. If declared within a function, it becomes a global variable or causes a reference error if used in strict mode.

  4. What is Use Strict?
    "use strict"; is a directive introduced in ECMAScript 5 that enables strict mode, which enforces stricter parsing and error handling rules in JavaScript, catching common coding errors and making it easier to write secure JavaScript.

  5. How to force developers to use Var keyword?
    You can't directly force developers to use var, but you can enforce coding standards and use linting tools like ESLint to catch violations.

  6. How can we handle Global Variables?
    You can limit the use of global variables by encapsulating code within functions or modules, using object properties to emulate namespaces, or adopting design patterns like the module pattern.

  7. How can we avoid Global variables?
    Avoid declaring variables in the global scope and instead encapsulate them within functions or modules. Use dependency injection or event-driven architectures to pass data between modules.

  8. What are Closures?
    Closures are functions that have access to the outer function's scope, even after the outer function has finished executing. They retain access to variables in their lexical scope.

  9. Why do we need Closures?
    Closures are useful for maintaining state, creating private variables and functions, implementing data hiding, and enabling functional programming patterns like currying and memoization.

  10. Explain IIFE?
    IIFE stands for Immediately Invoked Function Expression. It is a JavaScript function that is executed immediately after it is defined.

  11. What is the use of IIFE?
    IIFE is commonly used to create a new scope, encapsulate variables to avoid polluting the global scope, and execute code immediately without being stored in a variable.

  12. What is name collision in the global scope?
    Name collision occurs when two or more variables or functions in the global scope have the same name, leading to unexpected behavior or overwriting of values.

  13. IIFE vs Normal Function?
    IIFE is invoked immediately and does not pollute the global scope, while a normal function is defined and executed when called, and its variables may remain in scope until the function finishes executing.

  14. What are design patterns?
    Design patterns are reusable solutions to common problems encountered in software design. They provide a template for solving a particular issue in a consistent and efficient manner.

  15. Which is the most used design pattern?
    There isn't a single most used design pattern, as it depends on the problem being solved. However, some commonly used design patterns include Singleton, Factory, Observer, and MVC.

  16. What is the module Pattern and revealing module pattern?
    The module pattern is a design pattern used for creating encapsulated modules with private and public methods and variables. The revealing module pattern is a variation that explicitly reveals which methods and variables are public.

  17. What are the various ways to create JavaScript objects?
    JavaScript objects can be created using object literals, constructor functions, the Object.create() method, and ES6 classes.

  18. How can we do inheritance in JavaScript?
    Inheritance in JavaScript can be achieved using prototype chaining, constructor borrowing (call/apply), and ES6 classes.

  19. What is prototype in JavaScript?
    The prototype is an object from which other objects inherit properties and methods. Each JavaScript function has a prototype object that is shared among all instances created with that function.

  20. Explain Prototype chaining?
    Prototype chaining is the mechanism by which JavaScript objects inherit properties and methods from their prototype objects. Each object has a reference to its prototype, and if a property or method is not found on the object itself, JavaScript looks for it in the prototype chain.

  21. What is the Let Keyword?
    let is a keyword introduced in ECMAScript 6 for declaring block-scoped variables. Variables declared with let have block scope, which means they are only accessible within the block in which they are defined.

  22. Are Let variables hoisted?
    Variables declared with let are hoisted to the top of their block scope, but they are not initialized until the actual declaration is evaluated. This means you cannot access let variables before their declaration in the code.

  23. Explain Temporal Dead Zone?
    The Temporal Dead Zone (TDZ) is the period between entering a scope and the declaration being hoisted where variables declared with let and const exist but cannot be accessed. Accessing variables in the TDZ results in a ReferenceError.

  24. Let vs Var
    let and var are both used for variable declaration, but let has block scope, is not hoisted outside its block, and causes a ReferenceError if accessed before declaration, while var has function scope, is hoisted to the top of its function, and is accessible throughout the function.

  25. String Concatenation and Arithmetic puzzle
    This question seems to be missing. Could you provide more details or clarify?

Certainly, let's continue with the next set of questions:

  1. What is a class in ES6?
    In ES6 (ECMAScript 2015), a class is a type of function that allows for easier and more intuitive creation of objects with a blueprint for their properties and methods.

  2. So with the class Keyword does it imply JavaScript is an OOP language?
    JavaScript supports object-oriented programming (OOP) concepts like encapsulation, inheritance, and polymorphism, but it is not strictly a class-based OOP language like Java or C++. The introduction of the class keyword in ES6 made JavaScript more similar to class-based OOP languages.

  3. Differentiate between class and normal function?
    Classes in JavaScript provide a more structured way to create objects with constructors, methods, and inheritance, while normal functions can be used to define behavior but lack the built-in structure and inheritance capabilities of classes.

  4. What is an Arrow function?
    An arrow function is a concise syntax introduced in ES6 for writing anonymous functions. It uses the => syntax and has a shorter syntax compared to traditional function expressions.

  5. Why do we use Arrow functions?
    Arrow functions offer shorter syntax, lexical scoping of the this keyword, and do not bind their own this, arguments, super, or new.target like traditional functions, making them particularly useful for callbacks and functional programming.

  6. Differentiate between Arrow vs Normal Function?
    Arrow functions have a shorter syntax, do not bind their own this, arguments, super, or new.target, and cannot be used as constructors, while normal functions have more flexibility and can be used as constructors.

  7. Does an Arrow function create its own this?
    No, arrow functions lexically bind this to the surrounding scope, meaning they inherit the this value from the enclosing function or block scope.

  8. Explain Synchronous execution?
    Synchronous execution refers to the default behavior of JavaScript, where statements are executed line by line in the order they appear in the code, and each statement must complete before the next one begins.

  9. What is a Call Stack?
    The call stack is a data structure used by JavaScript to manage function invocations. It operates on a last-in, first-out (LIFO) basis and keeps track of the current function's execution context.

  10. What is a blocking call?
    A blocking call is a synchronous operation that halts the execution of further code until it completes. During a blocking call, the program cannot perform other tasks.

  11. How to avoid blocking calls?
    To avoid blocking calls, you can use asynchronous programming techniques such as callbacks, promises, or async/await to perform non-blocking operations and allow the program to continue executing while waiting for responses.

  12. Explain Asynchronous execution?
    Asynchronous execution allows tasks to be performed concurrently without blocking the main thread. Asynchronous operations are initiated and executed separately, and their results are processed once they become available.

  13. Sync vs Async?
    Synchronous operations block the execution until they complete, while asynchronous operations allow the program to continue executing while waiting for tasks to complete.

  14. How can we do Asynchronous calls?
    Asynchronous calls can be achieved using callback functions, promises, or async/await syntax, which allow for non-blocking execution and handling of asynchronous operations.

  15. What is a thread?
    A thread is the smallest unit of execution within a process. It represents a single sequence of instructions that can be scheduled for execution by the operating system.

  16. Explain Multi-threading?
    Multi-threading refers to the concurrent execution of multiple threads within a single process, allowing programs to perform multiple tasks simultaneously and utilize multiple CPU cores efficiently.

  17. Is JavaScript Multi-threaded?
    No, JavaScript is single-threaded, meaning it can only execute one set of instructions at a time. However, it can leverage asynchronous programming techniques to handle concurrent operations.

  18. Then how does setTimeout run?
    setTimeout is a browser API that schedules a function to be executed after a specified delay. It doesn't create a new thread; instead, it adds the function to the browser's event queue, which is processed after the current call stack is empty.

  19. What is a WebAPI/Browser API?
    Web APIs (also known as Browser APIs) are interfaces provided by web browsers that allow JavaScript to interact with the browser environment and perform tasks such as manipulating the DOM, making HTTP requests, and handling events.

  20. What is an Event Loop and Callback Queue?
    The event loop is a mechanism in JavaScript that continuously checks the call stack and the callback queue. If the call stack is empty, it takes the first callback from the queue and pushes it onto the stack for execution.

  21. Event loop and Callback code question ans
    This seems to be referencing a specific code question. Could you provide more details or clarify the question?

Top comments (1)

Collapse
 
jonrandy profile image
Jon Randy 🎖️ • Edited

What are different data types in JavaScript?

You've missed BigInt in your answer.

What are Closures?

The answer given is incorrect - closures are not functions, and ALL functions retain access to variables in their lexical scope. A closure is the combination of a function bundled together (enclosed) with references to its surrounding state.

String Concatenation and Arithmetic puzzle
This question seems to be missing. Could you provide more details or clarify?

Certainly, let's continue with the next set of questions:

Could I suggest perhaps better proofreading for your post? and possibly looking at these guidelines for suggestions on improving your post.