Blog
JavaScript

JavaScript Data Types and Type System

Understanding Primitive and Object Types

JavaScript has 8 Datatypes:

  1. String
  2. Number
  3. Bigint
  4. Boolean
  5. Undefined
  6. Null
  7. Symbol
  8. Object

The Object Datatype

The object data type can contain:

  1. An object
  2. An array
  3. A date
javascript
Note: A JavaScript variable can hold any type of data.

The Concept of Data Types

In programming, data types is an important concept. To be able to operate on variables, it is important to know something about the type. Without data types, a computer cannot safely solve this:

javascript

Does it make any sense to add "Volvo" to sixteen? Will it produce an error or will it produce a result? JavaScript will treat the example above as:

javascript
Note: When adding a number and a string. JavaScript will treat the number as string
javascript
Note: When adding a number and a string. JavaScript will treat the number as string
javascript

JavaScript evaluates expressions from left to right. Different sequences can produce different results:

javascript

Result

In the first example, JavaScript treats 16 and 4 as numbers, until it reaches "Volvo". In the second example, since the first operand is a string, all operands are treated as strings.

JavaScript Types are Dynamic

JavaScript has dynamic types. This means that the same variable can be used to hold different data types:

javascript

JavaScript Strings

A string (or a text string) is a series of characters like "John Doe". Strings are written with quotes. You can use single or double quotes:

javascript

You can use quotes inside a string, as long as they don't match the quotes surrounding the string:

javascript

JavaScript Numbers

All JavaScript numbers are stored as decimal numbers (floating point). Numbers can be written with, or without decimals:

javascript

Exponential Notation

Extra large or extra small numbers can be written with scientific (exponential) notation:

javascript

JavaScript BigInt

All JavaScript numbers are stored in a a 64-bit floating-point format.

JavaScript BigInt is a new datatype (ES2020) that can be used to store integer values that are too big to be represented by a normal JavaScript Number.

javascript

JavaScript Booleans

Booleans can only have two values: true or false.

javascript

Booleans are often used in conditional testing.

JavaScript Arrays

JavaScript arrays are written with square brackets.

Array items are separated by commas.

The following code declares (creates) an array called cars, containing three items (car names):

javascript

Array indexes are zero-based, which means the first item is [0], second is [1], and so on.

JavaScript Objects

JavaScript objects are written with curly braces {}. Object properties are written as “name:value” pairs, separated by commas.

javascript

The object (person) in the example above has 4 properties: firstName, lastName, age, and eyeColor.

The typeof Operator

You can use the JavaScript typeof operator to find the type of a JavaScript variable. The typeof operator returns the type of a variable or an expression:

javascript

Undefined

In JavaScript. a variable without a value, has the value undefined. The type is also undefined.

javascript

Any variable can be emptied, by setting the value to undefined. The type will also be undefined.

javascript

Empty Values

An empty value has nothing to do with undefined. An empty string has both a legal value and a type.

javascript
avatar

0 Comments

No comments

Leave a Reply

avatar

Recent Post

Related Topics

    Feeds

      Don't miss what's next 👋

      Enjoyed this content? Leave your email to get notified when we publish new insights, tutorials, and updates — no spam, ever.