JavaScript Built-in Types
https://www.dofactory.com/javascript/builtin-types
JavaScript Built-in Types
Value and Reference Types
Variables come in two different types: value types and reference types. Boolean values and numbers are value-based types, whereas strings, objects, arrays, and functions are reference types. Let's look at the differences.
Value types occupy a fixed size in memory. A Boolean value for example only takes one bit. All numbers in JavaScript are 64-bit floating point numbers, so each number is represented by 8 bytes. A reference type variable holds a reference to the value whose length is not fixed and can be of any size
Another difference between value and reference types is that value types are copied, passed, and compared by value. Reference types, are copied, passed, and compared by reference, rather than by value. Let's look at value types first:
Run
When variable y is assigned to x it copies its value and following the assignment any relationship between x and y is severed. So, changing the value of x has no impact on the value of y.
Consider this example where a reference type is copied:
Run
Following the execution of the second statement in which x is assigned to y, y refers to the same object that x refers to. JavaScript does not create a new copy of that object. If you make any changes to the underlying object through x, you can see these changes through y and vice-versa. Previous Next
String, Boolean, and Number
String type
A string represents an ordered sequence of zero or more 16-bit values. Each value typically represents a Unicode character. Unicode is designed to represent all of the characters found in all human languages.
A string is wrapped in either double or single quotes. Adding a single-quote character within a string that is delimited by double quotes is no problem; the same is true for adding a double-quote character in a single-quote delimited string. Adding a single quote in the latter requires an escape character before the single quote, like so \'. Other common escape characters include \n = newline; \r = carriage return; \t = tab, and \\ = backslash.
A string literal can be split across one or more lines by placing an escape character \ (called a continuation character) at the end of each line except the last one. The \ character does not become a part of the string literal. Here are some examples of string literals:
Run
Note that using the continuation character is generally not recommended.
Here are some other string members you frequently use: length, and charAt.
Run
And here are some additional frequently used string methods: indexOf, lastIndexOf, toLowerCase, substring, replace, and split.
Run
When comparing two strings they are considered equal when they have the same length and the same sequence of characters in corresponding positions.
Run
JavaScript strings are immutable
When we say that strings are immutable it means that once created, the contents of a string is of fixed length and cannot be modified. String may appear mutable when you work with them but they are not.: they are immutable internally. JavaScript does not offer any method or property that lets you alter the contents of a string value.
Again, modifying the string may appear to work, but in fact, a new string is generated. In the following example, b is referencing the same string as a. However, when modifying the underlying string through the b reference, a new string is created internally and its reference is assigned to b without affecting the original string referenced by a.
Run
String methods like replace and toLowerCase appear to alter the string on which they operate, but they also return new strings.
Boolean type
The Boolean type has two literal values: true and false. Note that true and false are language keywords and are not the same as 1 and 0.
All types of values in JavaScript have Boolean equivalents. An empty string, null, undefined, NaN, 0, -0, and false itself evaluate to the Boolean value false. All these types and their values are referred to as falsy. On the other hand, a non-empty string, any non-zero number including Infinity, any object, and true itself evaluate to the Boolean value true. These types and their values are referred to as truthy. Although truthy and falsy are unofficial terms, they are widely used in the JavaScript community.
JavaScript has a special Boolean casting function that is used to convert a value into its Boolean equivalent – essentially determining whether the passed in expression is truthy or falsy.
Run
Automatic conversions to Boolean equivalents are common in conditional statements.
Run
The first if statement returns true and therefore the alert displays. The second if-statement returns false and does not get executed.
String, Boolean, and Number properties are read-only
Variables of type string, number, and Boolean have properties and methods, but they are not objects. When you read a property or method from these types, JavaScript creates a temporary object, called a wrapper object, by calling the String(), Number(), or Boolean() constructor behind the scenes. Note that you can also explicitly call these constructor functions and create wrapper objects.
Run
When the second statement is executed, JavaScript creates a temporary String wrapper object. Then it adds a user-defined property called howlong on it, sets its value, and subsequently discards that object. This shows that String objects are not true objects because they do not accept new properties.
In summary, you cannot set properties to numbers, strings, or Booleans; any modification on the wrapper objects is not permanent.
Number-String conversions
JavaScript offers three ways to explicitly convert a string to a number. They are the Number constructor function, parseFloat, and parseInt. The latter two functions have some special behavior. Here is an example using the Number constructor function which takes a string argument:
Run
The parseFloat function converts strings to floating point numbers. It ignores any non-numeric values that appear after numeric characters:
Run
The parseInt function converts string to integer values. It also ignores any trailing non-numeric values:
Run
Here are some additional conversion methods: parseInt also lets you specify the base of the number. If you want JavaScript to treat your numeric string as base 10 number, pass 10 as the second parameter to parseInt(). Pass 8 if the string is an octal number.
Run
When reporting monetary values, you are likely to perform number-to-string conversions. In the output string, you may like to standardize the number of decimal places by rounding the trailing digits or padding with zeros. The Number object offers methods that you can use to convert numbers to strings. For example, the toFixed method converts it to a string while keeping a number of decimals.
Run
Here are two additional conversion methods: toPrecision and toExponential.
Run
JavaScript Math Object
For mathematical computations you use the Math object. There is no need to create an instance of Math to access its properties and methods because it is a static object. You simply use the Math object directly and call its method or property names. Below are max, min and abs methods:
Run
Next are some trigonometry related items: the constant PI, and sin and cos.
Run
Finally some rounding methods:
Run
JavaScript Date Object
To perform date and time calculations you use the Date object. The Date object holds a date as the number of milliseconds that have passed since Jan 1 1970 12:00 AM UTC. If you create a new Date object it will be set to the current date and time in the browser.
Run
The time zone is determined by the system settings. In the example below, let's assume that the time zone where this code is run is set to Paris i.e. UTC + 1:00. Depending on your timezone your results will differ.
Run
The difference between two dates is returned in milliseconds.
Run
Here are examples of formatting date and time. Please note that your returns will differ depending on your timezone:
Run
Be careful when you enter an invalid date like January 32, 2011. In most of the browsers like IE, Firefox, it is silently changed to February 1.
Last updated