Special Values

  • This lesson explains JavaScript special values and their behavior.
  • Introduction to Special Values in JavaScript

    JavaScript provides some special values that represent unusual or exceptional numeric situations.

    Common special values include:

    • NaN

    • Infinity

    • -Infinity

    In this lesson, we will focus on:

    • NaN

    • isNaN()

    NaN

    What is NaN ?

    NaN stands for Not a Number.

    It is a special numeric value in JavaScript that represents a result which is not a valid number, even though its type is number.

    Simple Definition

    NaN is returned when a mathematical operation fails to produce a valid numeric result.

    Important Point

    typeof NaN;   // "number"

    Even though it means “Not a Number”, JavaScript treats NaN as a number type.

    When Does NaN Occur ?

    NaN occurs in situations such as:

    • Invalid mathematical operations

    • Converting non-numeric strings to numbers

    • Undefined or incorrect calculations

      Common Examples of NaN

    Common Scenarios That Produce NaN

    Different cases where JavaScript returns NaN

    // Example 1: Invalid Math Operation
    let result1 = 0 / 0;
    console.log(result1); // NaN
    
    // Example 2: Non-Numeric String Conversion
    let value = Number("hello");
    console.log(value); // NaN
    
    // Example 3: Arithmetic With Invalid Data
    let result2 = "abc" * 5;
    console.log(result2); // NaN
    • NaN is Not Equal to Itself

      console.log(NaN === NaN);   // false

      How to Check for NaN ?

      You should never check NaN like this:

      value === NaN   // Wrong

      Instead, JavaScript provides:

      • isNaN()

      isNaN()

      What is isNaN() ?

      isNaN() is a built-in JavaScript function used to check whether a value is NaN or cannot be converted into a valid number.

      Syntax of isNaN()

      isNaN(value);

      • Returns true if value is not a number

      • Returns false if value is a valid number

    Validate Numbers Using isNaN() Function

    Checks whether a value is not a number or cannot be converted into a valid number

    // Basic Examples
    console.log(isNaN(10));    // false (valid number)
    console.log(isNaN("10"));  // false (converted to number)
    console.log(isNaN("abc")); // true (not a number)
    • How isNaN() Works Internally

      isNaN():

      1. Tries to convert the value into a number

      2. Checks whether the result is NaN

    Internal Working of isNaN()

    Explains how isNaN converts values before checking if they are NaN

    // Conversion behavior
    console.log(isNaN("123"));    // false (converted to number)
    console.log(isNaN("123abc")); // true (cannot convert)
    
    // Tricky interview examples
    console.log(isNaN(""));        // false
    console.log(isNaN(" "));       // false
    console.log(isNaN(null));      // false
    console.log(isNaN(undefined)); // true
    • Explanation:

      • Empty strings and null convert to 0

      • undefined converts to NaN

    Validating User Input Using isNaN()

    Checks if user input is a valid number before performing operations

    let input = "50";
    
    if (isNaN(input)) {
      console.log("Invalid number");
    } else {
      console.log("Valid number");
    }
    • Common Mistakes

      • Comparing values directly with NaN

      • Misunderstanding typeof NaN

      • Forgetting that isNaN() does type conversion

        Important Note for Modern JavaScript

        There is also:

        • Number.isNaN()

        Difference:

        • isNaN() converts values

        • Number.isNaN() checks strictly

        Example:

        isNaN("abc");           // true

        Number.isNaN("abc");    // false

        Number.isNaN(NaN);      // true

        This will be covered in advanced lessons.

        Best Practices

        • Never compare using === NaN

        • Use isNaN() for user input validation

        • Understand type conversion behavior

        • Prefer Number.isNaN() in modern code