This article is reprinted from null, undefined and Boolean values

Null, and Undefined

An overview of the

Null and undefined can both mean “nothing” and have very similar meanings. Assigning a variable to undefined or null makes almost no difference syntactically, to be honest.

var a = undefined;
/ / or
var a = null;
Copy the code

In the above code, variable A is assigned to undefined and null, respectively, which are almost equivalent.

In an if statement, they are both automatically returned to false, and the equality operator (==) even reports that they are equal.

if (!undefined) {
  console.log('undefined is false');
}
// undefined is false

if (!null) {
  console.log('null is false');
}
// null is false

undefined= =null
// true
Copy the code

From the code above, you can see how similar the two behave! Dart, Google’s alternative JavaScript language, specifies null and undefined.

Since the meaning and usage are similar, why set two of these values at the same time? It has to do with history.

When JavaScript was born in 1995, it was originally set to null for “none” just like Java. According to C tradition, NULL can be automatically converted to 0.

Number(null) / / 0
5 + null / / 5
Copy the code

In the code above, when null is converted to a number, it automatically becomes 0.

But Brendan Eich, the designer of JavaScript, feels that this is not enough. First of all, in the first version of JavaScript, NULL was treated as an object, just like in Java, and Brendan Eich felt that the value for “nothing” had better not be an object. Second, JavaScript at the time did not include error handling, and Brendan Eich felt that if NULL was automatically turned to zero, it would be hard to spot errors.

So he made another undefined. The difference is that null is an object representing “empty” and is zero when converted to a value. Undefined is a raw value that means “not defined here”. When converted to a value, it is NaN.

Number(undefined) // NaN
5 + undefined // NaN
Copy the code

Usage and Meaning

Null and undefined can be understood roughly as follows.

Null means null, that is, the value there is now null. When a function is called and no value is set for a parameter, null is passed to indicate that the parameter is null. For example, if a function accepts an error thrown by the engine as an argument, it passes null to indicate that no error occurred if no error occurred during execution.

Undefined means “undefined.” Here is a typical scenario for returning undefined.

// The variable is declared, but not assigned
var i;
i // undefined

// When calling a function, the argument that should be provided is not provided, which is equal to undefined
function f(x) {
  return x;
}
f() // undefined

// The object has no assigned attributes
var  o = new Object(a); o.p// undefined

If the function returns no value, undefined is returned by default
function f() {}
f() // undefined
Copy the code

Boolean

Boolean values represent both true and false states. “True” is represented by the keyword true, “false” is represented by the keyword false. Booleans have only these two values.

The following operators return Boolean values:

  • Pre-logic operators:! (Not)
  • Equality operator:= = =.! = =.= =.! =
  • Comparison operators:>.> =.<.< =

If JavaScript expects a location to be a Boolean, existing values at that location are automatically converted to Booleans. The conversion rule is that all values are considered true except for the following six values that are converted to false.

Expecting a location to be a Boolean refers to:

  • A == B
  • if (A) else if (B)
  • while (A)
  • undefined
  • null
  • false
  • 0
  • NaN
  • ""or' '(Empty string)

Booleans are often used to control program flow. See an example.

if (' ') {
  console.log('true');
}
// There is no output
Copy the code

In the code above, the criterion after the if command is expected to be a Boolean value, so JavaScript automatically converts the empty string to a Boolean value false, causing the program to not enter the code block, so there is no output.

Note that the Boolean values for the empty array ([]) and the empty object ({}) are both true.

if ([]) {
  console.log('true');
}
// true

if ({}) {
  console.log('true');
}
// true
Copy the code