Skip to content
Sandny Blog
  • Home
  • Java
  • JavaScript
  • AWS
  • Embedded
  • About
globals undefined and null values in javascript JavaScript

05 Global, null and undefined values in Javascript

  • September 21, 2022September 23, 2022
  • by Coder Kai

Globals, undefined and null values are special concepts that we need to know when working with Javascript. Especially what both undefined and null means when you encounter it running the program. Globals are also an important concept that may give you an understanding of different attributes, the scope and variables that can be created and accessed when programming

1. null value in Javascript

null represents that a value is absent but it is done deliberately. It means that when programmatically you want to make clear a variable assignment best to use the null value. By that when going through the next steps the programmer will understand that the null value is explicitly set by a decision prior to that particular place. This is the main difference between undefined and null.

As an example

const user = {firstName: 'John', age: 18, relationships: null};
if (user.isRegistered === undefined) {
// isRegistered is not defined in the object.
// Which means that the object will not always consider having that property
}
if (user.relationships === null) {
// this value is initialized to null.
// which means it is set to null deliberately and that should be a property of the user
}
const user = {firstName: 'John', age: 18, relationships: null}; if (user.isRegistered === undefined) { // isRegistered is not defined in the object. // Which means that the object will not always consider having that property } if (user.relationships === null) { // this value is initialized to null. // which means it is set to null deliberately and that should be a property of the user }
const user = {firstName: 'John', age: 18, relationships: null};

if (user.isRegistered === undefined) {
  // isRegistered is not defined in the object.
  // Which means that the object will not always consider having that property
}

if (user.relationships === null) {
  // this value is initialized to null.
  // which means it is set to null deliberately and that should be a property of the user
}

The typeof null returns an object which would represent that the null is a no object value. It can represent strings and numbers when there is no value as well. null is also a reserved keyword in Javascript.

2. undefined value in Javascript

The undefined value represents an uninitialized, unintended, or not considered value for different places.
For example:

When you define a variable without initializing to a value

let a; // not initialized
console.log(typeof a === 'undefined'); // prints true
console.log(a) // prints undefined
let a; // not initialized console.log(typeof a === 'undefined'); // prints true console.log(a) // prints undefined
let a; // not initialized
console.log(typeof a === 'undefined'); // prints true
console.log(a) // prints undefined

When you do not return any value from a function or pass a value to the parameter

const printNameWithoutReturn = (name) => {
console.log(name)
}
const checkName = printNameWithoutReturn('Foo');
console.log(typeof checkName === 'undefined'); // prints true
printNameWithoutReturn(); // prints undefined
const printNameWithoutReturn = (name) => { console.log(name) } const checkName = printNameWithoutReturn('Foo'); console.log(typeof checkName === 'undefined'); // prints true printNameWithoutReturn(); // prints undefined
const printNameWithoutReturn = (name) => {
  console.log(name)
} 

const checkName = printNameWithoutReturn('Foo');
console.log(typeof checkName === 'undefined'); // prints true

printNameWithoutReturn(); // prints undefined

undefined is a read-only(ECMA5) global value rather than a keyword. The typeof undefined returns 'undefined' as a string which means that the language has defined this as a special type.

null and undefined are both falsy values which means

null == undefined // true
null === undefined // false
null == undefined // true null === undefined // false
null == undefined // true
null === undefined // false

There are no methods or wrappers for null and undefined. So dot . operation or accessors [] would not work with those two as values or keywords.

3. isNil function from Lodash

Sometimes people find it annoying to check both undefined and null in javascript without checking for falsy values. If Lodash or Underscore libraries are present, you can use a function like isNil, or create a global function that behaves like isNil to cover both scenarios. If it comes to this point we can assume that the code is not well structured but it can come in handy for large codebases with enhancements.(Don’t take a plus point to using isNil anywhere as conceptually it could go wrong in many cases. This is added in case you find it somewhere when coding)

For example:

var isNil = (value) => value === null || value === undefined;
const user = {firstName: 'John', age: 18, relationships: null, balance: 0};
if (isNil(user.isRegistered)) {
// isRegistered is not defined and it will proceed here
}
if (isNil(user.relationships)) {
// isRegistered is defined as null and it will proceed here
}
if (user.balance) {
// here the balance is 0 but its a valid value.
// 0 is a falsy value for conditions and it will return a boolean when compared
// hence its not accurate to test a number for the if condition
}
if (isNil(user.balance)) {
// here it checks the balance is undefined or null
// It will not proceed as the value is 0
}
var isNil = (value) => value === null || value === undefined; const user = {firstName: 'John', age: 18, relationships: null, balance: 0}; if (isNil(user.isRegistered)) { // isRegistered is not defined and it will proceed here } if (isNil(user.relationships)) { // isRegistered is defined as null and it will proceed here } if (user.balance) { // here the balance is 0 but its a valid value. // 0 is a falsy value for conditions and it will return a boolean when compared // hence its not accurate to test a number for the if condition } if (isNil(user.balance)) { // here it checks the balance is undefined or null // It will not proceed as the value is 0 }
var isNil = (value) => value === null || value === undefined;

const user = {firstName: 'John', age: 18, relationships: null, balance: 0};

if (isNil(user.isRegistered)) {
  // isRegistered is not defined and it will proceed here
}

if (isNil(user.relationships)) {
  // isRegistered is defined as null and it will proceed here
}

if (user.balance) {
  // here the balance is 0 but its a valid value. 
  // 0 is a falsy value for conditions and it will return a boolean when compared
  // hence its not accurate to test a number for the if condition
}

if (isNil(user.balance)) {
  // here it checks the balance is undefined or null
  // It will not proceed as the value is 0
}

4. Globals in Javascript

The language provides its own set of values, libraries, helpers, and different extensions for ease of use. This is what we refer to as the global object. Attributes of this object are globally defined for the javascript runtime it can be accessed everywhere.

For example:

  1. References undefined, NaN, Infinity
  2. Functions parseInt(), encodeURI()
  3. Classes Date, String, Array
  4. Object libraries: Math, JSON

For client-side (browsers mostly) the global object is defined as window

Example

window.parseFloat() this can be just accessed as parseFloat()
window.parseFloat() this can be just accessed as parseFloat()
window.parseFloat() this can be just accessed as parseFloat()

In the global scope where there isn’t any closures or not inside function or class, global can be accessed as this keyword.

Example

this.parseFloat()
this.parseFloat()
this.parseFloat()

5. Declaring properties in the global scope

You can declare a variable in global scope and it cannot be deleted.

var globalProperty = 'I am a global property'
delete globalProperty
// returns false where it cannot be deleted.
// You can set the value again
var globalProperty = 'I am a global property' delete globalProperty // returns false where it cannot be deleted. // You can set the value again
var globalProperty = 'I am a global property'

delete globalProperty 
// returns false where it cannot be deleted. 
// You can set the value again

But you can always delete a variable that is initialized without var in none “strict” mode

globalProperty = 'I am a global property'
delete globalProperty
// returns true where it can be deleted
globalProperty = 'I am a global property' delete globalProperty // returns true where it can be deleted
globalProperty = 'I am a global property'
delete globalProperty
// returns true where it can be deleted

04. Javascript Guide Booleans and Equality
06. Wrapper objects in Javascript
Coder Kai
A humble developer
difference between null and undefined global variable in javascript how to define globals in javascript null in javascript undefined in javascript

Related articles

Multiple refs for an array of React Elements
Using multiple refs for an…
Immutable and Mutable Values in Javascript
07. Immutable and Mutable Values…
wrapper objects in javascript
06. Wrapper objects in Javascript
Javascript Booleans and Equality
04. Javascript Guide Booleans and…
How to add Chakra UI Animations
Chakra UI Animations
SSL Websocket using Nginx Proxy
SSL Websocket proxy with Nginx…
Change python version correctly
Python is not setting correct…
optimize React.js load time
How to optimize React.js app…
Multiple refs for an array of React Elements
How to use IntersectionObserver to…
Multiple refs for an array of React Elements
How to dismiss dropdowns when…
Javascript guide Strings
03. Javascript Guide – Strings
How to fetch data with useEffect
How to fetch data with…
add styles to stripe elements
How to add styles to…
Typescript
How to use Typescript with…
how to optimize react-native map view
How to optimize react-native map…
debounce with react hooks
Avoid multiple clicks using debounce…
Numbers inJavascript
02. Javascript Guide – Numbers
Introduction to Javascript
01. Javascript Guide – Introduction…
Nginx Load Balancer with docker…
Create React App Using npx

Categories

  • android 3
  • Apollo Client 1
  • AWS 8
    • AppSync 5
    • EC2 1
    • EKS 1
    • Route53 1
    • S3 1
  • AWS Amplify 1
  • Chakra UI 1
  • Docker 1
  • Embedded 1
  • EmberJS 1
  • FCM 1
  • Godaddy 1
  • GraphQL 3
  • ios 1
  • Jasper 1
  • Java 10
    • Java 11 1
    • Java 14 1
  • JavaEE 2
  • JavaScript 39
    • Express.js 4
    • Javascript Guide 7
    • Node.js 3
    • react-native 4
    • React.js 17
    • Typescript 1
  • Kubernetes 1
  • machine learning 1
  • Maven 2
  • OCaml 3
  • PostgreSQL 1
  • Python 2
  • react-native 4
  • ReactJS 3
  • sass 1
  • Server 6
  • spark 1
  • Terraform 2
  • Ubuntu 4
  • Uncategorized 1
  • webpack 2

Recent Comments

  • binance Sign Up on How to host GoDaddy domain with AWS EC2
  • binance on Android/iOS React-native heap limit allocation failed error
  • seo 2025 buyhacklink.com on How to create Lambda function with Terraform

Meta

  • Log in
  • Entries feed
  • Comments feed
  • WordPress.org

Archives

  • October 2022 3
  • September 2022 7
  • May 2022 1
  • December 2021 1
  • August 2021 1
  • July 2021 6
  • June 2021 3
  • February 2021 1
  • July 2020 1
  • December 2019 5
  • November 2019 6
  • October 2019 3
  • August 2019 1
  • March 2019 1
  • February 2019 1
  • January 2019 2
  • December 2018 1
  • September 2018 2
  • August 2018 1
  • June 2018 1
  • February 2018 1
  • November 2017 2
  • October 2017 5
  • September 2017 1
  • June 2017 1
  • May 2017 10
Sandny Blog space
Theme by Colorlib Powered by WordPress