Data Types

Data Types

The types are divided in primitives and objects.

Primitives

Primitive types are Immutable

  • number

  • string

  • boolean

  • symbol

  • object

  • undefined

Objects

Are values that represent a memory reference.

Anything that is not a primitive IS an object.

Wrapper

() can change the type data.

Number

Is a primitive and immutable, internally represented by IEEE 754 and is 64 bits.

Can handle values:

  • Decimal,

  • Octonal 0o | 0O,

  • Binary 0b | 0B,

  • Hexadecimal 0x | 0X.

Binary Operators

When using binary representation.

  • | Binary OR

  • & Binary AND

  • ^ Binary XOR

  • ~ Binary Negation

  • << Binary Shift Left

  • >> Binary Shift Right

  • >>> Binary Shift Right with Sign

Convertion Number -> String

(Number).toString( (10 | 16 | 2 | 8) )

Convertion String -> Number

All of these will convert a number in string to its value in number.

  • ~~string: [This is the binary operator, and will double invert]

  • +string

  • string - 0

  • string * 1

  • string / 1

With the exception of +:

  • "10" + 0 will do the concatanation

Decimal Operation residue

Doing math operation in decimals may result in weird results, due to residues due to rouding.

This happens because of the IEEE 754 number representation.

You can then use _lodash libraries for calculations, if you need decimals precision with calculations.

String

Are primitive and immutable, internally represented by Unicode UTF-16.

String Literals

Usefull string functions

.includes(substring): Checks for subtrings in the string.

.startsWith(substring): Checks for substring at the beginning of the string.

.endsWith(substring): Checks for substring at the end of the string.

.localeCompare(otherString): Do comparison of strings and returns -1, 0 or 1, ideal for sort.

.match(RegExp): Return parts of the string based on the RegExp.

.search(RegExp): Return the firts position in the string found by the RegExp.

  • Similar to .indexOf() but with RegExp.

.replace((RegExp | string), string): Return a new string, with replaced data.

.slice(InitialIdx, FinalIdx): Return part of the string parameters are [InitialIdx, FinalIdx).

  • FinalIdx could be negative, which will then count backwards.

.padStart(string) | .padEnd(string): Fill the string with char at the beginning or end.

.trim() | .trimLeft() | .trimRight(): Remove empty spaces.

Boolean

Are primitive and immutable, represented by true and false.

If created using the new Boolean() constructor they will be of type object.

Also new Boolean(false) will be created as true, this happens because of type coersion.

False values in Javascript

There are only 6 cases in javascript are evaluated to false:

Anything else will be evaluated to true.

Using logical operators for value attribuition

The return in logical operators are the own operands

  • if 1 || 2 will return 1

  • if true || 5 will return true

  • if false || 10 will return 10

  • if false && 5 will return false

Symbol

Are primitive, unique and immutable, and act as unique keys in objects.

Being uniques, you can never compare them because they will always be different.

They are used internally in for/of, string functions like match, split, replace, and others.

Regular Expressions (RegExp)

Can be used for validating strings, or extracting data from strings.

Usefull RegExp functions

.test(string): Check if passed string has the RegExp pattern.

.exec(string): Return an array of usefull information, with for instance extracted matched strings.

  • To be able to extract, inside the Pattern the section to be extracted must be envolved with (). Ex.: (\w+)@(\w+).com.br

Objects

Is a dynamic collection of properties defined by keys. These keys can ONLY be of type string | Symbol, other types will be converted.

The default primitive Object is considered immutable, because you cannot change its prototype.

undefined and null

undefined ideally means that whatever you are trying to access does not even exist.

null ideally means that the value of something is not set.

Deleting keys

You can delete keys with delete obj.key

Comparing Objects

The comparison of objects is done by reference, thus, you will be comparing their memory reference, which will always be different.

Heritage with (Prototypes)

You can have heritage in objects, just like in classes, with prototypes.

Although the key: value properties inherited by prototypes are NOT SHOWN in console.log or other functions.

Using for in will traverse not only the properties in the current object, but also in the __proto__.

in will check for property existance in the object AND __proto__.

__proto__: The key to access a reference to the object's prototype.

Every object created, has a default prototype Object.

This is why when you search for a unexistent key it returns undefined. It will try to access in the current object, if not found, it will go to the object __proto__ and look there. If it doesnt find it again it will again look into __proto__ of that upper object and look there. It will do this until reaches the default prototype object, and then return undefined.

.hasOwnProperty(key): Check if a property key exists in the CURRENT object, it will NOT look in __proto__.

Duplicated properties in own-object and its __proto__, the own-object property is returned and shadows the __proto__ one.

You can access the __proto__ one with own-object.__proto__.property.

Object API

Object.assign(targetObj, {}, {}, ...): To make a shallow copy multiple objects into one.

  • Similar to Spread Operator, thus, having nested objects will implicate in the copies of these properties being copies of its reference, and not hard copies of its data.

Object.defineProperty(obj, key, { value, ...opts }): Add a property with configuration options.

opts:

  • configurable: That a property be erased. [If a property can be deleted from the object with delete]

  • enumerable: That a property be enumered. [Will not show on console.log, and other funcs that show keys - It will be 'hidden']

  • value: Define the value for this property.

  • writable: That a property have its value altered. [Change values for this property]

Object.preventExtensions(obj): Forbit that the object have new properties, but allow the edit or remove of existent ones.

Object.isExtensible(obj): Check if the preventExtensions is enabled.

Object.seal(obj): Forbit that the object have new properties and remove existent, but allow edit of existent ones.

Object.freeze(obj): Forbit that the object have new properties, edit and remove of existent ones.

JSON

Data types that are accepted values in JSON.

  • number

  • string

  • boolean

  • object

  • array

  • null

Last updated