Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NULL and UNDEFINED #14

Open
Jaynil1611 opened this issue Jul 4, 2020 · 0 comments
Open

NULL and UNDEFINED #14

Jaynil1611 opened this issue Jul 4, 2020 · 0 comments

Comments

@Jaynil1611
Copy link
Owner

NULL

What is null?

null is an assignment value. It can be assigned to a variable as a representation of no value:

There are two features of null you should understand:

  • null is an empty or non-existent value.
  • null must be assigned.

In the example below, we are assigning value to null.

let value = null;
console.log(value);
// null 
  • null expresses a lack of identification, indicating that variable points to no object
  • When you assign null to a variable, you are declaring that this value is explicitly empty.

UNDEFINED

What is undefined?

Undefined usually means a variable has been declared, but not defined.

  • Another common scenario, where you will encounter an undefined is while trying to access a property of an object that doesn’t exist.
let myObject = {};
console.log(myObject.value);
// undefined
  • To summarize,
    • An object is declared but not defined/initialized.
    • Accessing an object property or an array index that does not exist.
    • Calling a function, without it’s required function parameters.

What is similar?

  • Both null and undefined are primitive values in JavaScript.
  • Another thing to note is that when comparing null and undefined they are equal.
null == undefined
// true
  • This is because both of them are considered falsy values in JavaScript.

Differences null vs. undefined

  • Arithmetic Operations

Another interesting difference to note is when arithmetic operations are performed with null vs. undefined.

Arithmetic operations with a null value will result in integer value while any arithmetic operation with undefined will result in the value of the variable being changed to NaN.

let a = 5 + null;
console.log(a);
// 5

let b = 5 + undefined;
console.log(b);
// NaN 
  • Equality

  • Typeof Null and Undefined

  • Interestingly enough, when using typeof to test null, it returns an object:
console.log(typeof(undefined));  //"undefined"
console.log(typeof(null));       //"object"
  • Comparison Using Abstract and Strict Equality

  • Since these are different data types, if we compare them with strict equality ===, we get false.
  • But if we compare them with abstract equality ==, we get true.
console.log(null === undefined)    //false
console.log(null == undefined)     //true
console.log(null !== undefined)    //true

Summary

  • null is an assigned value. It means nothing.
  • undefined typically means a variable has been declared but not defined yet.
  • null and undefined are falsy values.
  • null and undefined are both primitives. However, an error shows that typeof null = object.
  • null !== undefined but null == undefined.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant