Friday, September 15, 2023

Javascript Interview Questions

 JavaScript interview questions are a crucial part of technical interviews for web development roles. These questions assess a candidate's understanding of JavaScript fundamentals, coding skills, and problem-solving abilities. Common topics include data types, functions, closures, asynchronous programming, DOM manipulation, and ES6 features. Preparing for these questions is essential for aspiring developers to demonstrate their expertise in the language and secure web development positions


  1. What are the data types in JavaScript
    1. JavaScript is a dynamically typed language, which means variables can hold values of any data type without explicitly specifying the type. JavaScript has several basic or primitive data types, as well as complex or reference data types. Here are the primary data types in JavaScript:


      Primitive Data Types:


      1.Number Represents numeric values, including integers and floating-point numbers. Examples: `42`, `3.14`.


      2. String: Represents text or sequences of characters. Examples: `"Hello, World!"`, `'JavaScript'`.


      3. Boolean: Represents true or false values. Examples: `true`, `false`.


      4. Undefined: Represents a variable that has been declared but has not been assigned a value. Example: `let x;`.


      5. Null: Represents the intentional absence of any object value. Example: `let y = null;`.


      6. Symbol (ES6): Represents a unique and immutable value primarily used as object property keys. Example: `const sym = Symbol('description');`.


      7. BigInt (ES11): Represents large integers that cannot be represented by the `Number` type. Examples: `1234567890123456789012345678901234567890n`.


      Reference Data Types:


      8. Object: Represents a collection of key-value pairs, where keys are strings (or Symbols) and values can be of any data type. Examples: `{ name: 'John', age: 30 }`, `new Date()`.


      9. Array: A special type of object that represents an ordered list of values. Examples: `[1, 2, 3]`, `['apple', 'banana', 'cherry']`.

      1. Function: Functions are objects in JavaScript, and they can be assigned to variables, passed as arguments, and returned from other functions. Examples: `function add(a, b) { return a + b; }`.


      11. RegExp (Regular Expression): Represents a pattern used for searching or manipulating strings. Example: `/pattern/`.


      Special Data Types:


      12. NaN (Not-a-Number): Represents a special numeric value that indicates an unrepresentable or undefined value resulting from an invalid mathematical operation. Example: `NaN`.


      13. Infinity and -Infinity: Represent positive and negative infinity, respectively, which are used to represent values that are greater than or less than any finite number. Examples: `Infinity`, `-Infinity`.


  2. What is negative infinity
  3. What are the all types of pop-up boxes available in Javascript
    1. Alert Dialog
    2. Confirm Dialog
    3. Prompt Dialog
  4. Difference between an alert box and a confirmation box?
    1. The confirm dialog is used to display a message and ask the user for confirmation.
    2. It has two buttons: "OK" and "Cancel."
    3. The alert dialog is used to display a simple message to the user.
    4. It has a single "OK" button to close the dialog.
  5. What is the use of the blur function ?
    1. In JavaScript, the blur() function is used to remove focus from a specified HTML element. When an element has focus, it means that it is currently selected or "active" for user interaction, such as typing in an input field or navigating through form controls using the keyboard.

      The blur() function can be called on various types of HTML elements, such as form inputs, buttons, links, or any element that can receive focus. When you call blur() on an element, it triggers the following actions:

      • Removes Focus: The element loses focus, meaning it is no longer considered the currently active element for user interaction. If it's an input field, the cursor will no longer blink inside the field.
      • Triggers the blur Event: Calling blur() programmatically on an element also triggers the blur event for that element, allowing you to execute specific code when the element loses focus.
  6. Is Javascript case-sensitive?
    1. Yes
  7. What are the distinct types of error name values ?
    1. Error: This is the generic error type. It serves as the base type for all other error types.

      • name: "Error"
    2. SyntaxError: Occurs when there is a syntax error in the code, such as a missing closing parenthesis or semicolon.

      • name: "SyntaxError"

    3. TypeError: Occurs when a value is not of the expected type, or when an operation is performed on an inappropriate data type.

      • name: "TypeError"

    4. ReferenceError: Occurs when a variable or function is referenced before it is declared or out of scope.

      • name: "ReferenceError"

    5. RangeError: Occurs when an operation results in a value outside of the valid range.

      • name: "RangeError"

    6. URIError: Occurs when there is an issue with URI (Uniform Resource Identifier) handling, such as malformed URIs.

      • name: "URIError"

    7. EvalError: Historically, this error was used to represent errors in the eval() function. However, modern JavaScript engines have deprecated it, and it's rarely used.

      • name: "EvalError"

    8. Custom Errors: You can also create custom error types by extending the Error object or any of its subclasses and providing a custom name value.

  8. Difference between var and let ?
    1. var: Variables declared with var have function-level or global scope. This means they are accessible throughout the entire function or script in which they are declared.
    2. let: Variables declared with let have block-level scope. They are confined to the nearest enclosing block (typically denoted by curly braces {}), such as a loop, conditional statement, or a function.
    3. var: Variables declared with var are hoisted to the top of their containing function or script. This means that you can use a var variable before it's declared, but it will be initialized with undefined.
    4. let: Variables declared with let are also hoisted, but they are not initialized. If you try to access a let variable before it's declared, you'll get a ReferenceError.
  9. What difference between "==" and "===" ?
    1. "==" (Equality Operator):

      • The "==" operator is used for loose equality comparison.
      • It compares values for equality after performing type coercion if the operands have different types.
      • If the operands have different data types, JavaScript will attempt to convert one or both values to a common type before making the comparison.
      • It returns true if the values are equal (in value), false otherwise.
    2. "===" (Strict Equality Operator):

      • The "===" operator is used for strict equality comparison.
      • It compares values for equality without performing type coercion.
      • It returns true only if both the values and their data types are equal.
  10. Difference between const and let ?
    1. const
      1. Variables declared with const must be assigned a value when they are declared.
      2. Once assigned, the value of a const variable cannot be changed or reassigned. It is a constant.
      3. Attempting to reassign a const variable will result in a syntax error.
    2. let:
      1. Variables declared with let can be assigned a value when declared or left uninitialised.
      2. let variables can be reassigned with new values as many times as needed.

No comments:

Post a Comment

Javascript Interview Questions

  JavaScript interview questions are a crucial part of technical interviews for web development roles. These questions assess a candidate...