Day 0: Data Types
Data Types
JavaScript's Data Types
The latest ECMAScript standard defines seven data types:
- A primitive value or data type is data that is not an object and has no methods. All primitives are immutable, meaning they cannot be changed. There are six primitive types:
- Number
- String
- Boolean
- Symbol
- Null
- Undefined
- The seventh data type is Object
Number Data Type
According to the ECMAScript standard, all numbers are double-precision 64-bit binary format IEEE 754-2008, meaning there is no specific type for integers.
Maximum Value for a Number
The MAX_VALUE
property has a value of approximately . Values larger than Number.MAX_VALUE
are represented as Infinity
.
Minimum Value for a Number
The MIN_VALUE
property is the smallest positive value of the Number type closest to , not the most negative number, that JavaScript can represent. MIN_VALUE
has a value of approximately . Values smaller than Number.MIN_VALUE
("underflow values") are converted to .
Symbolic Numbers
There are three symbolic number values:
Infinity
: This is any number divided by , or an attempt to multiplyNumber.MAX_VALUE
by an integer .-Infinity
: This is any number divided by , or an attempt to multiplyNumber.MAX_VALUE
by an integer .NaN
: This stands for "Not-a-Number" and denotes an unrepresentable value (i.e., ).
The isSafeInteger Method
A safe integer is an integer that:
- Can be exactly represented as an IEEE-754 double precision number, and
- Whose IEEE-754 representation cannot be the result of rounding any other integer to fit the IEEE-754 representation.
The Number.isSafeInteger()
method determines whether the provided value is a number that is a safe integer.
Maximum Safe Integer
The Number.MAX_SAFE_INTEGER
constant has a value of , or .
Minimum Safe Integer
The Number.MIN_SAFE_INTEGER
constant has a value of , or .
var var1 = 1;
var var2 = 0;
var var3 = -0;
console.log("1 / 0 = " + var1 / var2);
console.log("1 / -0 = " + var1 / var3);
console.log("MAX_VALUE: " + Number.MAX_VALUE);
console.log("MAX_VALUE + 1 = " + Number.MAX_VALUE * 2);
console.log("MIN_VALUE = " + Number.MIN_VALUE);
console.log("MIN_VALUE - 1 = " + Number.MIN_VALUE - 1);
console.log("MAX_SAFE_INTEGER = " + Number.MAX_SAFE_INTEGER);
console.log("MIN_SAFE_INTEGER = " + Number.MIN_SAFE_INTEGER);
console.log("SquareRoot(-1) = " + Math.sqrt(-1));
console.log("isSafeInteger(100) = " + Number.isSafeInteger(100));
String Data Type
A string value is a chain of zero or more Unicode characters (i.e., letters, digits, and punctuation marks) that we use to represent text. We include string literals in our scripts by enclosing them in single ('
) or double ("
) quotation marks. Double quotation marks can be contained in strings surrounded by single quotation marks (e.g., '"'
evaluates to "
), and single quotation marks can be contained in strings surrounded by double quotation marks (e.g., "'"
evaluates to '
). The following are examples of strings:
Notice that JavaScript does not have a type to represent a single character. To represent a single character in JavaScript, you create a string that consists of only one character. A string that contains zero characters ("") is an empty (zero-length) string.
Unlike in languages like C, JavaScript strings are immutable. This means that once a string is created, it is not possible to modify it. However, it is still possible to create another string based on an operation on the original string. For example:
- A substring of the original by picking individual letters or using
String.substr()
. - A concatenation of two strings using the concatenation operator (
+
) orString.concat()
.
Boolean Data Type
A boolean represents a logical entity and can have one of two literal values: true
, and false
.
Symbol Data Type
Symbols are new to JavaScript in ECMAScript Edition 6. A Symbol is a unique and immutable primitive value and may be used as the key of an Object property.
Null Data Type
The null data type is an internal type that has only one value: null
. This is a primitive value that represents the absence of any object value. A variable that contains null contains no valid number, string, boolean, array, or object. You can erase the contents of a variable (without deleting the variable) by assigning it the null value.
Undefined Data Type
The undefined value is returned when you use an object property that does not exist, or a variable that has been declared, but has never had a value assigned to it.
The typeof Operator
As demonstrated in some of the code examples above, we can use the typeof
operator to determine the type associated with a variable's current value:
typeof
.
Variables
Dynamic Typing
JavaScript is a loosely typed or dynamic language, meaning you don't need to declare a variable's type ahead of time and the language autmatically determines a variable's type while the program is being processed. That also means that you can reassign a single variable to reference different types. For example:
Naming
JavaScript is a case-sensitive language, meaning that a variable name such as myVariable
is different from the variable name myvariable
. Variable names can be of any length, and the rules for creating legal variable names are as follows:
- The first character must be either an ASCII letter (uppercase or lowercase) or an underscore (
_
). Note that a number cannot be used as the first character. - Subsequent characters can be ASCII letters, underscores, or digits (e.g., the numbers through ).
- The variable name must not be a reserved word.
The code below declares some valid variable names:
var _daysCount
var MinimumCost
var page10
var Total_elements
The following declarations are invalid variable names and will not compile:
// This will produce "SyntaxError: Unexpected number"
var 10Students
// This will produce "SyntaxError: Unexpected token &"
var First&Second
Declaration and Initialization
The first time a variable appears in your script is considered its declaration. The first mention of the variable sets it up in memory, and the name allows you to refer back to it in your subsequent lines of code. You should declare variables using the var
keyword before using them. If you do not initialize a variable that was declared using the var
keyword, it automatically takes on the value undefined
.
Coercion
In JavaScript, you can perform operations on values of different types without raising an exception. The JavaScript interpreter implicitly converts, or coerces, one of the data types to that of the other, then performs the operation. The rules for coercion of string, number, or boolean values are as follows:
- If you add a number and a string, the number is coerced to a string.
- If you add a boolean and a string, the boolean is coerced to a string.
- If you add a number and a boolean, the boolean is coerced to a number.