As mentioned previously,ECMAScript has five primitive types: Undefined, Null, Boolean, Number, and String. ECMA-262 defines the term type as a set of values, and each of the primitive types defines a range of values it can contain as well as literal representations of that type.To determine if a value is in the range of values for a particular type, ECMAScript provides the typeof operator.This operator can be used to determine if a value represents a primitive type and if so, which primitive type it represents.
The typeof operator
The typeof operator takes one parameter: the variable or value to check.For example:
Calling typeof on a variable or value returns one of the following values:
The Undefined type
As previously mentioned, the Undefined type has only one value, undefined. When a variable is declared and not initialized, it is given the value of undefined by default.
The previous line of code declares a variable named oTemp, which has no initialization value. This variable is given a value of undefined, which is the literal representation of the Undefined type.You can test that the variable is equal to the literal yourself by running this code snippet:
This code displays an alert with the word “true”,indicating that these two values are indeed equal.You can also use the typeof operator to show that the variable has a value of undefined.
Note that a variable having the value of undefined is different from a value being undefined.However,the typeof operator doesn’t actually distinguish between the two. Consider the following:
The previous code outputs “undefined” for both variables, even though only one of them (oTemp2) is undefined.If you try to use oTemp2 with any operator other than typeof, it causes an error becauseoperators can only be applied to defined variables. For example, this causes an error:
The value undefined is also returned when a function doesn’t explicitly return a value, as in the following:
The Null type
Another type with just one value,the Null type, has only the special value null,which is also its literal.The value undefined is actually a derivative of the value null,so ECMAScript defines them as equal to each other.
Even though the values are both true, they are considered to have different meanings. Whereas undefined is the value assigned when a variable is declared and not initialized, null is the value used to represent an object that doesn’t exist (which I touched upon briefly in the discussion of the typeof operator).If a function or method is supposed to return an object, it usually returns null when the object isn’t found.
The Boolean type
The Boolean type is one of the most frequently used in the language.It has two values, true and false (which are also the two Boolean literals). Even though false isn’t equal to 0, 0 is converted to false when necessary, making it safe to use either in a Boolean statement.
The Number type
The most unique type defined in ECMA-262 is the Number type. The Number type can represent both 32-bit integer and 64-bit floating-point values. A Number type literal is considered any number entered directly (not accessed from another variable). For example, the following line of code declares a variable to hold an integer value, which is defined by the literal 55:
Integers can also be represented as either octal (base 8) or hexadecimal (base 16) literals. For an octal literal, the first digit must be a zero (0), and the following digits can be any octal digit (0 through 7), as in this line of code:
To create a hexadecimal literal, the first digit must be a zero (0) followed by the letter x, followed by any number of hexadecimal digits (0-9 and A-F). The digits may be in uppercase or lowercase. For example:
To define a floating-point value, you must include a decimal point and one digit after the decimal point (for instance, use 1.0 not 1.). This is considered a floating-point number literal. Example:
The interesting thing about this form of floating-point literal is that it is actually stored as a string until it’s needed for calculation.
For very large or very small numbers, floating-point values can be represented using e-notation. In e-notation, a number is represented by digits (including decimal digits), followed by an e (or an E), followed by the number of times to multiply it by 10. Confused? Here’s an example:
This notation represents the number 31250000. You can get this value by converting the e-notation to a calculation: 3.125 107, which is exactly equal to 3.125 10 10 10 10 10 10 10.
E-notation can also be used to represent very small numbers, such as 0.00000000000000003, which can be written as 3e-17 (here, 10 is raised to the –17 power, meaning that you will actually be dividing by 10 17 times). ECMAScript, by default, converts any floating-point number with six or more leading zeros into e-notation.
A few special values are also defined as part of the Number type. The first two are Number.MAX_VALUE and Number.MIN_VALUE,which define the outer bounds of the Number value set. All ECMAScript numbers must fall between these two values, without exception.A calculation can,however, result in a number that does not fall in between these two numbers.
When a calculation results in a number greater than Number.MAX_VALUE,it is assigned a value of Number.POSITIVE_INFINITY, meaning that it has no numeric value anymore.Likewise a calculation that results in a number less than Number.MIN_VALUE is assigned a value of Number.NEGATIVE_ INFINITY,which also has no numeric value.If a calculation returns an infinite value, the result cannot be used in any further calculations.
Because an infinite number can be positive or negative, a method can be used to determine if a number is finite (instead of testing for each infinite number separately).The isFinite() method can be called on any number to ensure that the number isn’t infinite.For example:
The final special number value is NaN,which stands for Not a Number. NaN is an odd special value. In general, this occurs when conversion from another type (String, Boolean, and so on) fails. For example, trying to convert the word blue into a number value will fail because there is no numeric equivalent. Just like the infinity values, NaN cannot be used in mathematical calculations. Another oddity of NaN is that it is not equal to itself, meaning that the following will return false:
For this reason, it is not recommended to use the NaN value itself. Instead, the function isNaN() will do the job quite nicely:
The String type
The String type is unique in that it is the only primitive type that doesn’t have a definite size. A string can be used to store zero or more Unicode characters, represented by 16-bit integers (Unicode is an international character set)
Each character in a string is given a position, starting with the first character in position 0, the second character in position 1, and so on. This means that the position of the final character in a string is always the length of the string minus 1.
String literals are specified by using either double quotes (“) or single quotes (‘). This differs from Java, where double quotes are used to specify strings and single quotes are used to specify characters. However, because ECMAScript has no character type, it is permissible to use either notation. For example, the following two lines are valid:
The string type also encompasses several character literals, which should be very familiar to Java, C, and Perl developers.
Java Script Related Tutorials
|Adv Java Tutorial||Core Java Tutorial|
|Java-Springs Tutorial||Java Servlets Tutorial|
|EJB(Enterprise JavaBeans) Tutorial||JavaServer Faces (JSF) Tutorial|
|Java Swing Tutorial||Java Tutorial|
|JavaMail API Tutorial||Java 8 Tutorial|
|Java XML Tutorial||Java Bean Utils Tutorial|
|The Java Debugger (JDB) Tutorial||Java.math Package Tutorial|
Java Script Related Interview Questions
|Java Script Interview Questions||Adv Java Interview Questions|
|Core Java Interview Questions||Java-Springs Interview Questions|
|Java Servlets Interview Questions||EJB(Enterprise JavaBeans) Interview Questions|
|JavaServer Faces (JSF) Interview Questions||Java Swing Interview Questions|
|Java 8 Interview Questions||Java XML Interview Questions|
|JavaFX Interview Questions||The Java Debugger (JDB) Interview Questions|
Java Script Related Practice Tests
|Java Script Practice Tests||Adv Java Practice Tests|
|Core Java Practice Tests||Java-Springs Practice Tests|
|Java Servlets Practice Tests||EJB(Enterprise JavaBeans) Practice Tests|
|JavaServer Faces (JSF) Practice Tests||Java 8 Practice Tests|
Java Script Tutorial
Browser And Operating System Detection
All About Events
Advanced Dom Techniques
Forms And Data Integrity
Drag And Drop
Interacting With Plugins
All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd
Wisdomjobs.com is one of the best job search sites in India.