Global functions are functions that JavaScript gives you for free. You call them by name, parseInt("42", 10), with no object in front and nothing to import. They work anywhere in your code: in a browser, in Node.js, in Deno and in Bun.
They belong to the global object, which you can reach as globalThis. In a browser that object is window. There are 11 of them, and they do three kinds of jobs: turn text into numbers, check numbers, and encode or decode text for URLs. One of them, eval(), runs code that is written as text.
This page explains the idea. The Global Functions index links the full guide for each function, with syntax, three runnable examples and FAQs.
Two of them are deprecated.
escape() and unescape() are old and should not be used in new code. eval() still works, but it is risky. Use encodeURIComponent() and JSON.parse() instead.
Syntax
JavaScript Syntax
Call a global function by its name. Here are five of them at work:
JavaScript
console.log(parseInt("42px", 10)); // 42 text -> whole number
console.log(parseFloat("3.14abc")); // 3.14 text -> decimal number
console.log(isNaN("hello")); // true is it "not a number"?
console.log(isFinite(10 / 0)); // false Infinity is not finite
console.log(encodeURIComponent("a & b")); // a%20%26%20b safe for a URL
console.log(decodeURIComponent("a%20%26%20b")); // a & b
Reference
Core Ideas
Ideas every global function tutorial in this series builds on:
Idea
Example
Remember
Global function
parseInt("42", 10)
Called by name, with no object in front
globalThis
globalThis.parseInt
The global object that holds these functions (window in a browser)
Conversion
isNaN("42")
Some functions turn the value into a number before they test it
NaN
0 / 0
The result of a number calculation that has no answer
Infinity
10 / 0
The result of dividing by zero, or of a number that is too big
Radix
parseInt("ff", 16)
The number base used to read text, from 2 to 36
Percent code
%20
How a character is written inside a URL (%20 is a space)
Deprecated
escape("a b")
Still works, but you should not use it in new code
Compare
Global Function or Number Method?
Four of the global functions have a twin on the Number object. Only two of the pairs are really the same function:
Global function
Number version
Same function?
parseInt()
Number.parseInt()
Yes, they are the same function
parseFloat()
Number.parseFloat()
Yes, they are the same function
isNaN()
Number.isNaN()
No. The global one converts first, the Number one does not
isFinite()
Number.isFinite()
No. The global one converts first, the Number one does not
For checks, prefer Number.isNaN() and Number.isFinite(), because they never change the value first. isNaN("hello") is true only because the text is converted to NaN; Number.isNaN("hello") is false.
Behavior
How Global Functions Work
No object in front
Write the name and the parentheses: parseFloat("3.5"). You do not write Math. or Number. before it, and nothing needs to be imported.
They live on the global object
Each function is a property of globalThis, so globalThis.parseInt === parseInt is true. Be careful not to create your own variable with the same name, because it would hide the built-in function.
Text readers stop early
parseInt() and parseFloat() read from the left and stop at the first character that does not fit, so "42px" still gives 42. The result is NaN when the text does not start with a number.
Checks convert first
isNaN() and isFinite() turn the value into a number before they answer. That is why isNaN("") is false: empty text becomes 0.
typeof parseInt: function
On globalThis: true
Number.parseInt is the same: true
Number.isNaN is the same: false
isNaN('abc'): true
Number.isNaN('abc'): false
How It Works
They are functions.typeof parseInt is "function".
They live on the global object.globalThis.parseInt is the very same function as parseInt.
Some twins are identical.Number.parseInt and parseInt are one function with two names.
Some twins are different.Number.isNaN is a separate, stricter function. For the text "abc" the global one says true (after converting) and the Number one says false.
Continue
The Global Functions
There are 11 global functions. Each one has its own tutorial:
They are built-in functions that you can call by name from anywhere in your code, without an object in front and without an import. Examples are parseInt(), parseFloat(), isNaN(), isFinite(), encodeURIComponent() and eval(). They are properties of the global object, globalThis.
This series covers 11: decodeURI(), decodeURIComponent(), encodeURI(), encodeURIComponent(), escape(), eval(), isFinite(), isNaN(), parseFloat(), parseInt() and unescape(). Browsers add many more, such as setTimeout() and fetch(), but those belong to the browser, not to the JavaScript language.
None. They are the same function, so Number.parseInt === parseInt is true. The same is true for parseFloat() and Number.parseFloat(). isNaN() and isFinite() are different from Number.isNaN() and Number.isFinite(), because the global versions convert the value first.
Avoid escape() and unescape(), which are deprecated; use encodeURIComponent() and decodeURIComponent() instead. Avoid eval() unless you are sure the text is safe, because it runs any code it is given. For data, use JSON.parse().
Yes. The functions in this series are part of the JavaScript language itself, so they work the same in browsers, Node.js, Deno and Bun. In Node.js the global object is also called globalThis (or global).