Value and function Properties of the ECMAScript Global Object
ECMAScript Global Object – Part 1
Foreword: In this part of the series I talk about the value and function properties of the ECMAScript Global Object.
By: Chrysanthus Date Published: 21 Sep 2014
Remember, any ECMAScript object has properties. When the property holds a datum, it is called a data property or a value property. The property can also be a function. When the property is a function, it is called a method.
Note: If you cannot see the code or if you think anything is missing (broken link, image absent), just contact me at firstname.lastname@example.org. That is, contact me for the slightest problem you have about what you are reading.
Every computer language builds up. You need to learn something today in the language, and then use it to learn something at a higher level in the same language. This series is part of my volume, ECMAScript Course. At the bottom of this page, you have links to the different series you should have studied before reaching here.
Value Properties of the Global Object
The value properties are called, undefined, infinity and NaN.
The undefined Property
Remember, there is an ECMAScript language data type called, undefined, whose value is undefined. The global object has a property called undefined, and the value of the property is undefined.
The NaN Property
The global object has a property called NaN. The value of this property is NaN. It means whatever value you are talking of, is not a number.
The Infinity Property
The value of this property is +∞. I will say more about this property later.
Note: With the global object, you do not have to type its identifier and a dot before you type its value property, in order to use the property.
Function Properties of the Global Object
When a property of an ECMAScript object is a function, it is called a method. So, these function properties of the global object are methods of the global object. You do not have to type its identifier and a dot before you type the method, in order to use the method. These methods (functions) are available for use (call), anywhere in your script. I will spend the rest of this tutorial talking about some of these functions (global methods).
The parseInt Function
This function converts a string to a whole number (integer). The syntax of the function is:
parseInt (string , radix)
where string is the string to be converted and radix should either be 10 or 16 for base 10 or base 16 number, respectively. radix is also optional, and when optional, it is base 10. Read and try the following code:
wholeNo = parseInt(" 457 ");
The extra HTML code produces a web page. In the script, the string " 457 " has been converted to the integer, 457. For a good conversion, there should be no alphabet (A, a, B, b, etc.) in the string. In the conversion, any leading and trailing whitespace (r, , r, t, etc.) is removed
Note: if the number literal within the string has a decimal part, as in 98.712 for example, the decimal part (.712) will be truncated in the conversion – no rounding takes place.
A float number is a number with a decimal point, e.g. 26.85 .The parseFloat function is similar to the parseInt function but it converts a string to a float instead of an integer. The syntax is:
where string is the string needing conversion and has within it a decimal (base 10) literal. Any leading or trailing whitespace next to the literal is removed. For a good conversion, there should be no alphabet (A, a, B, b, etc.) in the string. Read and try the following code:
floatNo = parseFloat(" 26.85 ");
In the script, the string " 26.85 " has been converted to the integer, 26.85.
The isNaN Function
This function returns Boolean true if the argument coerces to NaN; otherwise it returns false. Remember, NaN means “Not a Number”. The syntax of the function is:
where the argument, exp is what you want to check if it is not a number.
Read and try the following code:
ident = isNaN("text text");
The output is true. The function, isNaN (exp) should be read as follows: is exp not a number?
An example of a URL is:
A URL or URI like this is typically sent from the client browser to a web server in the Internet. In this URL the website address is:
The file name the ULR accesses at the server is:
After ? in the URL, you have the data or query string needed by the file, where firstNm means First Name, lastNm means Last Name. There are three name/value pairs in this URL: you have first name and its value, last name and its value and email and its value. The value for first name is John; the value for last name is Smith and the value for email is email@example.com
Now there are certain characters in a URL that are reserved characters having special meanings: meaning you cannot use any of them in the text of the website address; you cannot use any of them in the text of a filename in the URL, you cannot use any of them in a name in the query string, you cannot use any of them in a value in the query string. The reserved characters are:
; / ? : @ & = + , $
Such characters separate the URL into meaningful components. With this, you can see that there is a problem with the email value in the URL above. The email value has the reserved character, @. The string as it is cannot be transmitted successfully, unless the @ in the email value (address) is coded. If you code the @ in the email value to ‘%40’ without the quotes, then the URL will be transmitted successfully.
Now, considering the reserved characters, for successful transmission of a URL, any ‘;’ in a name or value of the URL has to be coded (changed) into %3B; any / has be to coded into %2F; any : has to be coded into %3A; any @ has to be coded into %40; any & has to be coded into %26; any = has to be coded into %3D; any + has to be coded into %2B; any , has to be coded into %2C; and any $ has to be coded into %24.
where uriComponent is a URL name or value. Read and try the following code that illustrates the use of the encodeURIComponent() global method:
URLPart = "http://www.pung.com/see.php?firstNn=John&lastNn=Smith&email="
emailVal = "firstname.lastname@example.org";
emailEncoded = encodeURIComponent(emailVal);
fullURL = URLPart + emailEncoded;
The output is:
Note that in the final URL, email@example.com has been coded to jsmth%40ya.com ., where @ has been replaced by %40 .
In the script, the first statement creates a string made up of the left large part of the URL, leaving out just the email value to complete the URL. The second statement creates a string for the email value, as the user would type it. The third statement encodes the email value and returns the encoded string. The fourth statement adds the encoded email value to the left half of the URL. The last statement displays (writes to web page) the complete URL with the email value coded.
The decodeURIComponent Function
This function does the opposite of the encodeURIComponent function. It replaces any code in a name or value component of a URL with the reserved character. The syntax is:
where encodedURIComponent is the name or value having the coded character. Read and try the following code that illustrates the use of the function with the coded email value, jsmth%40ya.com :
codedEmail = "jsmth%40ya.com";
uncodedEmail = decodeURIComponent(codedEmail);
The output is:
You can see that %40 has been changed to @.
That is it for this part of the series. We stop here and continue in the next part.
Related LinksECMAScript Basics
Expressions and Statements in ECMAScript
Custom Object in ECMAScript
ECMAScript Global Object
The ECMAScript Array
The ECMAScript String Object
ECMAScript Regular Expressions
ECMAScript Date Object
ECMAScript Error Basics
Number Objects in ECMAScript
Inheritance in ECMAScript
Advanced ECMAScript Regular Expressions
More Related Links
Major in Website Design
Web Development Course