Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”

Determines if a character is unique

null

Null means “no object”, meaning there should be no value

  1. typeof null = object
  2. Converts to a value of 0
  3. As arguments to a function, indicating that the function’s arguments are not objects; As the end of the object prototype chain
null >= 0; //true 

null <= 0; //true 

null == 0; //false 

null > 0; //false 

null < 0; //false

null === 0; //false
Copy the code

undefined

Undefined means the original value of “none” or “missing value”, that is, there should be a value here, but it is not defined yet

  1. typeof undefined = undefined

  2. Convert the value to NaN

  3. For example, if a variable is declared but not assigned, it equals undefined; Function returns undefined by default; The object has no assigned attribute, the value of which is undefined

Null is an object representing "none" and is zero when converted to a value; Undefined is a primitive value for "none", which is NaN when converted to a value.Copy the code

Why null

First, NULL is treated as an object, just like in Java. However, JavaScript data types fall into primitive and complex types, and Brendan Eich felt that a value for “nothing” is best not an object.

  • The original type

    • Number
    • String (string)
    • Boolean (Boolean)
  • Synthesis of type

    • Object
    • Array
    • Functions

Second, the earliest versions of JavaScript did not include error handling, and when a data type mismatch occurred, the type was automatically converted or silently failed. If null is automatically converted to 0, errors are hard to spot


Thank you very much for reading this. If this article is good or helpful to you, please like it, follow it and share it