Subscribe to my email list now at http://jauyeung.net/subscribe/
Follow me on Twitter at https://twitter.com/AuMayeung
Many more articles at https://medium.com/@hohanga
let x = 1; x = 'bar'; x = true;
In the first line
x is a number, but in the second line, the same variable
x has been reassigned into a string. In the last line, it has been again been reassigned, this time to a Boolean.
false and represents logical entities. The null type only has one value, which is
The largest and smallest available values for a number are
-Infinity respectively. We can also use the constants
Number.MIN_VALUE to represent the largest and smallest numbers. We can use the
1/+0 // Infinity 1/-0 // -Infinity
n character to the end of a number. With BigInt, we can make calculations that have results beyond the safe range of normal numbers. For example, we can write the following expressions and still get the numbers we expect:
const x = 2n ** 55n; const y = x + 1n;
x we get
36028797018963968n and for
y we get
36028797018963969n , which is what we expect. BigInts can use the same arithmetic operations as numbers like
%. A BigInt behaves like a number when converted to a Boolean with functions, keywords, or operators like
!. BigInts cannot be operated in the same expressions as numbers. If we try that, we will get a
Strings are used to represent textual data. Each element of the string has its own position in the string. It’s zero-indexed, so the position of the first character of a string is 0. The
length property of the string has the total number of characters of the string.
substr() function and use the
concat() function to concatenate two strings.
We should only present text data with strings. If there are more complex structures needed for your data structure, then they shouldn’t be represented with a string. Instead, they should be objects. This is because it’s easy to make mistakes with strings since we can put in the characters we want. Therefore, mistakes are made easily.
Symbols are new to ES2015. It is a unique and immutable identifier. Once you have created it, it cannot be copied. Every time you create a new symbol, it’s a unique one. Symbols are mainly used for unique identifiers in an object. That’s a symbol’s only purpose.
There are some static properties and methods of its own that expose the global symbol registry. It is like a built-in object, but it doesn’t have a constructor, so we can’t write
new Symbol to construct a symbol object with the
To create new symbols, we can write:
const fooSymbol = Symbol('foo')
Note that each time we call the
Symbol function, we get a new symbol, so if we write
Symbol('sym') === Symbol('sym')
the expression above would be
Object is a reference data type, which means it can be referenced by an identifier that points to the location of the object in memory. In memory, the object’s value is stored, and, with the identifier, we can access the value. Object has properties, which are key-value pairs with the values being able to contain the data with primitive types or other objects. That means we can use object to build complex data structures. The key is an identifier for the values of a property, which can be stored as a string or symbol. There are two types of properties that have certain attributes in an object. Objects have data properties and accessor properties.
[[Value]]— This can be of any type. It has the value retrieved by a getter of the property. Defaults to
[[Writable]]— This is a Boolean value. If it’s
[[Value]]can’t be changed. Defaults to
[[Enumerable]]— This is a Boolean value. If it’s
true, then it can be iterated over by the
for...inloop, which is used to iterate over the properties of an object. Defaults to
[[Configurable]]— This is a Boolean value. If it’s
true, then the property can be deleted or changed to an accessor property, and all attributes can be changed. Otherwise, the property can’t be deleted or changed to an accessor property, and attributes other than
[[Writable]]can’t be changed. Defaults to
[[Get]]— This is either a function or it’s
undefined. This may contain a function that is used to retrieve a property value whenever a property is being retrieved. Defaults to
[[Set]]— This is either a function or it’s
undefined. This lets us set the assigned value to an object’s property whenever an object’s property is attempted to be changed. Defaults to
[[Enumerable]]— This is a Boolean value that defaults to
false. If it’s
true, then the property will be included when we loop through the properties with the
[[Configurable]]— This is a Boolean value that defaults to
false. If it’s
false, then we can’t delete the property and can’t make changes to it.
Date object built into the standard library, so we can use it to manipulate dates.
Array are also objects.
length property to get the size of the array. The
Array object has many convenient methods to manipulate arrays, like the
push method to add items to the end of the array and the
indexOf method to find the index of the first occurrence of a given value. A
TypedArray object is an object that lets us see an array-like view of a binary data buffer.
Since ES2015, the following typed array objects are available:
Int8Array, value ranges from -128 to 127
Uint8Array, value ranges from 0 to 255
Uint8ClampedArray, value ranges from 0 to 255
Int16Array, value ranges from -32768 to 32767
Uint16Array, value ranges from 0 to 65535
Int32Array, value ranges from -2147483648 to 2147483647
Unit32Array, value ranges from 0 to 4294967295
Floar32Array, value ranges from -1.2 times 10 to the 38 to 3.4times 10 to the 38
Float64Array, value ranges from 5.0 times 10 to the 324 to 1.8 times 10 to the 308
BigInt64Array, value ranges from -2 to the 63 to 2 to the 63 minus 1
BigUint64Array, value ranges from 0 to 2 to the 64 minus 1
Since ES2015, we have new iterable object types. They are
WeakSet represent sets of objects, and
WeakMap represent objects with a list of key-value pairs.
Map keys can be iterated over, but
WeakMap’s keys cannot be.
typeof operator, we can determine the type of a variable or a given value.
typeof operator to determine the data type of your variable or value.