Javascript global object: JavaScript implements a global object which holds a kit of properties, functions, and objects that are accessed globally, without a namespace. Want to learn more about the JavaScript Global Object Variables, Functions & Properties? Then, this tutorial is the perfect one for all beginners and experienced programmers. So, dive into this page and directly get into the topic using the direct links available below.
- What is a Global Object in JavaScript?
- Window object in the Browser
- JavaScript Global Properties
- JavaScript Global Functions
What is a Global Object in JavaScript?
Objects in JavaScript are distinct from the Global Object. Applying the new operator, you cannot build global objects. When the scripting engine is initialized then only it comes into existence. Once the initialization is completed, the functions and constants are ready to use while coding in JavaScript.
A global object enables you to perform the below conditions −
- It provides access to built-in functions and values. Call alert immediately like the below code snippet, with window −
alert("Demo Text"); // or window.alert("Demo Text");
- Even, it also provides access to global function declarations and
var
variables in JavaScript. Look at the below code –
<script> var str = "Demo Text"; // using window alert( window.str ); </script>
Some of the JavaScript Global objects are:
- Array
- Boolean
- Date
- Function
- JSON
- Math
- Number
- Object
- RegExp
- String
- Symbol
and errors:
- Error
- EvalError
- RangeError
- ReferenceError
- SyntaxError
- TypeError
- URIError
Window object in the Browser
In the Browser, the window
object is the Global Object. Any JavaScript Global Variables or Functions can be accessed as properties of thewindow
object while programming.
Do Refer:
JavaScript Global Properties
The properties of JavaScript Global Object are:
- Infinity
- NaN
- undefined
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 never 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 testFunc = () => {} testFunc() //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 cat === 'undefined') { }
JavaScript Global Functions
The functions are:
- decodeURI()
- decodeURIComponent()
- encodeURI()
- encodeURIComponent()
- eval()
- isFinite()
- isNaN()
- parseFloat()
- parseInt()
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://google.com/good morning/") //"http://google.com/good%20morning/"
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://www.example.org/a file with spaces.html") // "http%3A%2F%2Fwww.example.org%2Fa%20file%20with%20spaces.html"
eval()
This is a special function that takes a string that contains JavaScript code and evaluates/runs it.
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.20', 10) //10.2 parseFloat('10.81', 10) //10.81
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.20', 10) //10 parseInt('10.81', 10) //10