JavaScript Tutorial/Number Data Type/Introduction
Содержание
- 1 Built-in Values
- 2 JavaScript primitive values, Booleans, numbers, and strings, are pseudo-objects
- 3 NaN stands for Not a Number
- 4 Number.MAX_VALUE and Number.MIN_VALUE define the outer bounds of the Number value set
- 5 Number() Object
- 6 Numbers
- 7 Primitive and Reference Values
- 8 Primitive Types
- 9 Reference Types
- 10 Special Numerical Values
- 11 The Boolean Type
- 12 The Null Type
- 13 The Number Type
- 14 The Undefined Type
- 15 Variables are loosely typed.
Built-in Values
Numerical Constants Provided by JavaScript
Math Constant Description Math.E Base of natural logarithms Math.LN2 Natural log of 2 Math.LN10 Natural log of 10 Math.LOG2E Base 2 log of e Math.LOG10E Base 10 log of e Math.PI Pi Math.SQRT1_2 Square root of 1/2 Math.SQRT2 Square root of 2
JavaScript primitive values, Booleans, numbers, and strings, are pseudo-objects
They actually have properties and methods.
var sColor = "blue";
alert(sColor.length); //outputs "4"
NaN stands for Not a Number
NaN is an odd special value.
In general, this occurs when conversion from another type fails.
NaN cannot be used in mathematical calculations.
NaN is not equal to itself.
alert(NaN == NaN); //outputs "false"
Number.MAX_VALUE and Number.MIN_VALUE define the outer bounds of the Number value set
All numbers must fall between Number.MAX_VALUE and Number.MIN_VALUE.
When a calculation results in a number greater than Number.MAX_VALUE, it is assigned a value of Number.POSITIVE_INFINITY.
When a calculation results in a number less than Number.MIN_VALUE, it is assigned a value of Number.NEGATIVE_ INFINITY.
If a calculation returns an infinite value, the result cannot be used in any further calculations.
There is a special value for infinity named Infinity.
Number.POSITIVE_ INFINITY has a value of Infinity.
Number.NEGATIVE_INFINITY has a value of -Infinity.
A method can be used to determine if a number is finite.
var iResult = iNum* some_really_large_number;
if (isFinite(iResult)) {
alert("Number is finite.");
} else {
alert("Number is infinite.");
}
Number() Object
Syntax
var variable = new Number(value)
The Number object represents numeric value types.
You can create a Number object by specifying a value in the parameter for the number constructor.
Properties and Methods of the Number Object
Property/Method Description MAX_VALUE Specifies the largest value a number can have. MIN_VALUE Specifies the smallest value a number can have without being equal to 0. NaN Stands for Not a Number. Represents a value that is not equal to any numeric value. NEGATIVE_INFINITY A special value that represents a negative infinity value. POSITIVE_INFINITY A special value that represents a positive infinity value. prototype Represents the prototype for the number class. toSource() Returns a string representation of the number object. toString() Returns a string representing the specified number object. valueOf() Returns the primitive value of a number object as a number data type.
Numbers
Every number in JavaScript is treated as a floating-point number.
JavaScript does support integers, octal, hexadecimal, and so on.
At the lowest level, JavaScript sees numbers as floating-point numbers.
5. 1. Introduction 5. 1. 1. Numbers 5. 1. 2. <A href="/Tutorial/JavaScript/0100__Number-Data-Type/NumberObject.htm">Number() Object</a> 5. 1. 3. <A href="/Tutorial/JavaScript/0100__Number-Data-Type/Variablesarelooselytyped.htm">Variables are loosely typed.</a> 5. 1. 4. <A href="/Tutorial/JavaScript/0100__Number-Data-Type/BuiltinValues.htm">Built-in Values</a> 5. 1. 5. <A href="/Tutorial/JavaScript/0100__Number-Data-Type/SpecialNumericalValues.htm">Special Numerical Values</a> 5. 1. 6. <A href="/Tutorial/JavaScript/0100__Number-Data-Type/PrimitiveandReferenceValues.htm">Primitive and Reference Values</a> 5. 1. 7. <A href="/Tutorial/JavaScript/0100__Number-Data-Type/PrimitiveTypes.htm">Primitive Types</a> 5. 1. 8. <A href="/Tutorial/JavaScript/0100__Number-Data-Type/TheUndefinedType.htm">The Undefined Type</a> 5. 1. 9. <A href="/Tutorial/JavaScript/0100__Number-Data-Type/TheNullType.htm">The Null Type</a> 5. 1. 10. <A href="/Tutorial/JavaScript/0100__Number-Data-Type/TheBooleanType.htm">The Boolean Type</a> 5. 1. 11. <A href="/Tutorial/JavaScript/0100__Number-Data-Type/TheNumberType.htm">The Number Type</a> 5. 1. 12. <A href="/Tutorial/JavaScript/0100__Number-Data-Type/NumberMAXVALUEandNumberMINVALUEdefinetheouterboundsoftheNumbervalueset.htm">Number.MAX_VALUE and Number.MIN_VALUE define the outer bounds of the Number value set</a> 5. 1. 13. <A href="/Tutorial/JavaScript/0100__Number-Data-Type/NaNstandsforNotaNumber.htm">NaN stands for Not a Number</a> 5. 1. 14. <A href="/Tutorial/JavaScript/0100__Number-Data-Type/JavaScriptprimitivevaluesBooleansnumbersandstringsarepseudoobjects.htm">JavaScript primitive values, Booleans, numbers, and strings, are pseudo-objects</a> 5. 1. 15. <A href="/Tutorial/JavaScript/0100__Number-Data-Type/ReferenceTypes.htm">Reference Types</a>
Primitive and Reference Values
A variable can hold one of two types of values: primitive values and reference values.
Primitive values are data that are stored on the stack.
Primitive value is stored directly in the location that the variable accesses.
Reference values are objects that are stored in the heap.
Reference value stored in the variable location is a pointer to a location in memory where the object is stored.
Primitive types inlcude Undefined, Null, Boolean, Number, or String.
5. 1. Introduction 5. 1. 1. <A href="/Tutorial/JavaScript/0100__Number-Data-Type/Numbers.htm">Numbers</a> 5. 1. 2. <A href="/Tutorial/JavaScript/0100__Number-Data-Type/NumberObject.htm">Number() Object</a> 5. 1. 3. <A href="/Tutorial/JavaScript/0100__Number-Data-Type/Variablesarelooselytyped.htm">Variables are loosely typed.</a> 5. 1. 4. <A href="/Tutorial/JavaScript/0100__Number-Data-Type/BuiltinValues.htm">Built-in Values</a> 5. 1. 5. <A href="/Tutorial/JavaScript/0100__Number-Data-Type/SpecialNumericalValues.htm">Special Numerical Values</a> 5. 1. 6. Primitive and Reference Values 5. 1. 7. <A href="/Tutorial/JavaScript/0100__Number-Data-Type/PrimitiveTypes.htm">Primitive Types</a> 5. 1. 8. <A href="/Tutorial/JavaScript/0100__Number-Data-Type/TheUndefinedType.htm">The Undefined Type</a> 5. 1. 9. <A href="/Tutorial/JavaScript/0100__Number-Data-Type/TheNullType.htm">The Null Type</a> 5. 1. 10. <A href="/Tutorial/JavaScript/0100__Number-Data-Type/TheBooleanType.htm">The Boolean Type</a> 5. 1. 11. <A href="/Tutorial/JavaScript/0100__Number-Data-Type/TheNumberType.htm">The Number Type</a> 5. 1. 12. <A href="/Tutorial/JavaScript/0100__Number-Data-Type/NumberMAXVALUEandNumberMINVALUEdefinetheouterboundsoftheNumbervalueset.htm">Number.MAX_VALUE and Number.MIN_VALUE define the outer bounds of the Number value set</a> 5. 1. 13. <A href="/Tutorial/JavaScript/0100__Number-Data-Type/NaNstandsforNotaNumber.htm">NaN stands for Not a Number</a> 5. 1. 14. <A href="/Tutorial/JavaScript/0100__Number-Data-Type/JavaScriptprimitivevaluesBooleansnumbersandstringsarepseudoobjects.htm">JavaScript primitive values, Booleans, numbers, and strings, are pseudo-objects</a> 5. 1. 15. <A href="/Tutorial/JavaScript/0100__Number-Data-Type/ReferenceTypes.htm">Reference Types</a>
Primitive Types
JavaScript has five primitive types: Undefined, Null, Boolean, Number, and String.
Each of the primitive types defines a range of values as well as literal representations of that type.
To determine if a value is in the range of values for a particular type, JavaScript provides the typeof operator.
This operator can be used to determine if a value represents a primitive type.
5. 1. Introduction 5. 1. 1. <A href="/Tutorial/JavaScript/0100__Number-Data-Type/Numbers.htm">Numbers</a> 5. 1. 2. <A href="/Tutorial/JavaScript/0100__Number-Data-Type/NumberObject.htm">Number() Object</a> 5. 1. 3. <A href="/Tutorial/JavaScript/0100__Number-Data-Type/Variablesarelooselytyped.htm">Variables are loosely typed.</a> 5. 1. 4. <A href="/Tutorial/JavaScript/0100__Number-Data-Type/BuiltinValues.htm">Built-in Values</a> 5. 1. 5. <A href="/Tutorial/JavaScript/0100__Number-Data-Type/SpecialNumericalValues.htm">Special Numerical Values</a> 5. 1. 6. <A href="/Tutorial/JavaScript/0100__Number-Data-Type/PrimitiveandReferenceValues.htm">Primitive and Reference Values</a> 5. 1. 7. Primitive Types 5. 1. 8. <A href="/Tutorial/JavaScript/0100__Number-Data-Type/TheUndefinedType.htm">The Undefined Type</a> 5. 1. 9. <A href="/Tutorial/JavaScript/0100__Number-Data-Type/TheNullType.htm">The Null Type</a> 5. 1. 10. <A href="/Tutorial/JavaScript/0100__Number-Data-Type/TheBooleanType.htm">The Boolean Type</a> 5. 1. 11. <A href="/Tutorial/JavaScript/0100__Number-Data-Type/TheNumberType.htm">The Number Type</a> 5. 1. 12. <A href="/Tutorial/JavaScript/0100__Number-Data-Type/NumberMAXVALUEandNumberMINVALUEdefinetheouterboundsoftheNumbervalueset.htm">Number.MAX_VALUE and Number.MIN_VALUE define the outer bounds of the Number value set</a> 5. 1. 13. <A href="/Tutorial/JavaScript/0100__Number-Data-Type/NaNstandsforNotaNumber.htm">NaN stands for Not a Number</a> 5. 1. 14. <A href="/Tutorial/JavaScript/0100__Number-Data-Type/JavaScriptprimitivevaluesBooleansnumbersandstringsarepseudoobjects.htm">JavaScript primitive values, Booleans, numbers, and strings, are pseudo-objects</a> 5. 1. 15. <A href="/Tutorial/JavaScript/0100__Number-Data-Type/ReferenceTypes.htm">Reference Types</a>
Reference Types
Reference types are commonly referred to as classes.
Objects are created by using the new operator and providing the name of the class to instantiate.
For example, the following code creates an instance of the Object class:
var o = new Object();
Special Numerical Values
Number Constant Description Number.MAX_VALUE Largest representable number Number.MIN_VALUE Smallest representable number Number.NaN Not-a-number Number.POSITIVE_INFINITY Positive infinity Number.NEGATIVE_INFINITY Negative infinity
The Boolean Type
The Boolean type has two values, true and false.
True and false are also the two Boolean literals.
Even though false isn"t equal to 0, 0 is converted to false when necessary.
var bFound = true;
var bLost = false;
The Null Type
The Null type, has only the special value null.
NUll is also its literal.
The value undefined is actually a derivative of the value null, so JavaScript defines them as equal to each other.
alert(null == undefined); //outputs "true"
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.
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.
All mathematical operations return decimal results.
var iNum = 55;
The Undefined Type
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.
var oTemp;
alert(oTemp == undefined);
Variables are loosely typed.
variables in JavaScript are not given a specific type.
Each variable is defined using the var operator and can be initialized with any value.
var color = "red";
var num = 25;
var visible = true;