JavaScript Data Types

July 17, 2018
js data types objects primitives

The latest ECMAScript standard defines seven data types:

Let’s go over some important features one by one.

Number type

Unlike many other programming languages, JavaScript does not define different types of numbers, like integers, short, long, floating-point etc. JavaScript numbers are always stored as double precision floating point numbers, following the international IEEE 754 standard. Further reading see this article.

1 === 1.0 // true
(0.1+0.2) === 0.3 // false

In addition to being able to represent floating-point numbers, the number type has three symbolic values: +Infinity, -Infinity, and NaN (not-a-number).

Except for decimals, we can also use multiple number representations like:

String type

JS strings are groupings of characters and must be inside of either double or single quotes. Each element in the String occupies a position in the String. The first element is at index 0, the next at index 1, and so on. The length of a String is the number of elements in it.

var txt = "ABCDE";
txt.length; // 5
txt[2]; // 'C'

JS strings are immutable. This means that once a string is created, it is not possible to modify it.

Backslash escape character and escape sequences:

Boolean type

Boolean represents a logical entity and can have two values: true, and false. You can use the Boolean() function to find out if an expression (or a variable) is true. JavaScript uses Type Conversion to coerce any value to a Boolean in contexts that require it, such as conditionals and loops.

A falsy value is a value that translates to false when evaluated in a Boolean context. There are six kinds of falsy values (considered as false):

All the others are truthy values (Note: including empty array [] and empty object {}.)

Null vs Undefined

null variables exist and have null explicitly assigned to them.

undefined variables exist, but don’t have anything assigned to them.

Coding convention:

  1. If there’s an object not assigned yet, it’s better to assign a value null to it.

  2. If there’s a string/number/boolean/symbol not assigned yet, use undefined. Actually just declare the variable (without explicitly assign a value) like let xxx will do the work.

In order to know if a variable has been declared (value equals to undefined doesn’t mean undeclared), we can use:

// To test for variable a
if ('a' in window) {...}

Symbol type

Will be introduced later…

Object type

Objects can be seen as a collection of properties. Property values can be values of any type, including other objects, which enables building complex data structures. A property’s value can be a function, in which case the property is known as a method. Properties are identified using key values. A key value is either a String or a Symbol value.

Access or set object properties:

  1. square bracket notation: objectName[propertyName] or objectName['propertyName']

  2. dot notation: objectName.propertyName

An object property name can be any valid JavaScript string, or anything that can be converted to a string, including the empty string.

However, any property name that is not a valid JavaScript identifier (for example, a property name that has a space or a hyphen, or that starts with a number) can only be accessed using the square bracket notation. This notation is also very useful when property names are to be dynamically determined (when the property name is not determined until runtime).

When you use dot notation, JS is thinking you are looking for a key whose value is a string of whatever is after the dot. See this example:

let person = { name: "John", age: 18}
let x = 'name'
person.x // undefined
person[x] // "John"
person['x'] // undefined

You can use the bracket notation with for...in to iterate over all the enumerable properties of an object.

comments powered by Disqus