πŸ“˜
NavDoc by Bash School
GithubContact
πŸ“˜
NavDoc by Bash School
  • πŸŽ“Introduction
  • 🐒Getting Started
  • ⚑Changelog
  • πŸ‘¨β€πŸš€Maintainers
  • πŸ›£οΈRoadmap
  • Fundamentals
    • The Internet
      • Introduction
      • What is a URL
      • What is a port
      • The DNS protocol
      • The TCP protocol
      • The UDP protocol
      • The Web
      • The HTTP protocol
      • Hyperlinks
      • What is a Web browser
      • What is a Web server
    • HTML
      • Your first HTML page
      • Text tags
      • Attributes
      • Links
      • Images
      • Lists
      • Head Tags
      • Container tags
    • CSS
      • Introduction
      • Colors
      • selectors
      • Cascade
      • Specificity
      • Units
      • Advanced selectors
      • Typography
      • The box model
      • The display property
      • Responsive design
  • JavaScript
    • Basics
      • Introduction
      • Literals , Identifiers, Variables
      • Comments
      • The difference between let, const and var
      • Types
      • Operators and expressions
      • Arithmetic operators
      • The assignment operator
      • Operators precedence
      • Strings
      • Numbers
      • Semicolons, white space and sensitivity
      • Arrays
      • Conditionals
      • Loops
      • Functions
      • Objects
      • Arrays + functions
      • OOPS
      • Asynchronous
      • Scope, hoisting, event loop
      • ES Modules
      • Errors and exceptions
      • Built-in objects
        • The global object
        • Object properties
        • Number
        • String
        • Math
        • JSON
        • Date
        • Intl
        • Set and Map
      • More operators
    • Nodejs
      • Getting Started
      • Installation
      • Hello World in Node
      • Modules
      • Packages
      • File Handling
      • HTTP Request
      • Processing Files
      • HTTP
    • Express.js
      • Getting Started
      • Middleware
      • Serve Static Assets
      • How to Send Files to the Client
      • Sessions
      • Validate Input
      • Sanitizing Data
      • Forms
      • File Uploads
    • React
      • Setting up a React project with Vite
      • React Components
      • Introduction to JSX
      • Using JSX to compose UI
      • The difference between JSX and HTML
      • Embedding JavaScript in JSX
      • Handling user events
      • Managing state
      • Component props
      • Data flow
      • Lifecycle events
      • Managing forms in React
      • Install the React Developer Tools
      • Installing Tailwind CSS in a React app
      • Build a counter in React
    • TypeScript
      • Key Benefits
      • Types of Languages
      • The Need for TypeScript
      • What is TypeScript?
      • The tsc Compiler
      • Basic Types in TypeScript
      • tsconfig
      • Interfaces
      • Types
      • Arrays in TypeScript
      • Enums
      • Exporting and importing
    • MongoDB
      • SQL vs. NoSQL Databases
      • Installing MongoDB
      • MongoDB Databases and Collections
      • Working with Documents
      • MongoDB Operators
      • Sorting, Indexing & Searching
      • Built-in Methods
Powered by GitBook
On this page
  • Infinity
  • NaN
  • undefined
  • decodeURI()
  • decodeURIComponent()
  • encodeURI()
  • encodeURIComponent()
  • eval()
  • isFinite()
  • isNaN()
  • parseFloat()
  • parseInt()

Was this helpful?

Edit on GitHub
  1. JavaScript
  2. Basics
  3. Built-in objects

The global object

JavaScript provides a global object which has a set of properties, functions and objects that are accessed globally, without a namespace.

The properties are

  • Infinity

  • NaN

  • undefined

and the functions are

  • decodeURI()

  • decodeURIComponent()

  • encodeURI()

  • encodeURIComponent()

  • eval()

  • isFinite()

  • isNaN()

  • parseFloat()

  • parseInt()

The objects are the ones you already saw before, which are part of the standard library:

  • Array

  • Boolean

  • Date

  • Function

  • JSON

  • Math

  • Number

  • Object

  • RegExp

  • String

  • Symbol

and errors.

Let’s now describe here the global properties and functions.

Infinity

Infinity in JavaScript is a value that represents infinity.

Positive infinity. To get negative infinity, use the – operator: -Infinity.

Those are equivalent to Number.POSITIVE_INFINITY and Number.NEGATIVE_INFINITY.

Adding any number to Infinity, or multiplying Infinity for any number, still gives Infinity.

NaN

The global NaN value is an acronym for Not a Number. It’s returned by operations such as zero divided by zero, invalid parseInt() operations, or other operations.

parseInt() //NaN
parseInt('a') //NaN
0/0 //NaN

A special thing to consider is that a NaN value is ever equal to another NaN value. You must use the isNaN() global function to check if a value evaluates to NaN:

NaN === NaN //false
0/0 === NaN //false
isNaN(0/0) //true

undefined

The global undefined property holds the primitive value undefined.

Running a function that does not specify a return value returns undefined:

const test = () => {}
test() //undefined

Unlike NaN, we can compare an undefined value with undefined, and get true:

undefined === undefined

It’s common to use the typeof operator to determine if a variable is undefined:

if (typeof dog === 'undefined') {

}

decodeURI()

Performs the opposite operation of encodeURI()

decodeURIComponent()

Performs the opposite operation of encodeURIComponent()

encodeURI()

This function is used to encode a complete URL. It does encode all characters to their HTML entities except the ones that have a special meaning in a URI structure, including all characters and digits, plus those special characters:

~!@#$&*()=:/,;?+-_.

Example:

encodeURI("http://flaviocopes.com/ test/")
//'http://flaviocopes.com/%20test/'

encodeURIComponent()

Similar to encodeURI(), encodeURIComponent() is meant to have a different job.

Instead of being used to encode an entire URI, it encodes a portion of a URI.

It does encode all characters to their HTML entities except the ones that have a special meaning in a URI structure, including all characters and digits, plus those special characters:

  • _.!~*'()

Example:

encodeURIComponent("http://flaviocopes.com/ test/")
//'http%3A%2F%2Fflaviocopes.com%2F%20test%2F'

eval()

This is a special function that takes a string that contains JavaScript code, and evaluates / runs it.

This function is very rarely used and for a reason: it can be dangerous.

isFinite()

Returns true if the value passed as parameter is finite.

isFinite(1) //true
isFinite(Number.POSITIVE_INFINITY) //false
isFinite(Infinity) //false

isNaN()

Returns true if the value passed as parameter evaluates to NaN.

isNaN(NaN) //true
isNaN(Number.NaN) //true
isNaN('x') //true
isNaN(2) //false
isNaN(undefined) //true

This function is very useful because a NaN value is never equal to another NaN value. You must use the isNaN() global function to check if a value evaluates to NaN:

0/0 === NaN //false
isNaN(0/0) //true

parseFloat()

Like parseInt(), parseFloat() is used to convert a string value into a number, but retains the decimal part:

parseFloat('10,000', 10) //10     ❌
parseFloat('10.00', 10) //10     βœ… (considered decimals, cut)
parseFloat('10.000', 10) //10     βœ… (considered decimals, cut)
parseFloat('10.20', 10) //10.2     βœ… (considered decimals)
parseFloat('10.81', 10) //10.81     βœ… (considered decimals)
parseFloat('10000', 10) //10000  βœ…

parseInt()

This function is used to convert a string value into a number.

Another good solution for integers is to call the parseInt() function:

const count = parseInt('1234', 10) //1234

Don’t forget the second parameter, which is the radix, always 10 for decimal numbers, or the conversion might try to guess the radix and give unexpected results.

parseInt() tries to get a number from a string that does not only contain a number:

parseInt('10 lions', 10) //10

but if the string does not start with a number, you’ll get NaN (Not a Number):

parseInt("I'm 10", 10) //NaN

Also, just like Number it’s not reliable with separators between the digits:

parseInt('10,000', 10) //10     ❌
parseInt('10.00', 10) //10     βœ… (considered decimals, cut)
parseInt('10.000', 10) //10     βœ… (considered decimals, cut)
parseInt('10.20', 10) //10     βœ… (considered decimals, cut)
parseInt('10.81', 10) //10     βœ… (considered decimals, cut)
parseInt('10000', 10) //10000  βœ…
PreviousBuilt-in objectsNextObject properties

Last updated 1 year ago

Was this helpful?

I recommend to read on the subject.

this article