JavaScript Data Types and Examples | Dynamic Typing

JavaScript Data Types is not directly associated with any value type like other languages (Java, Python, etc) have int, String, etc. Any JavaScript Variable can be assigned and re-assigned the value. That why it’s called a Dynamic typing.

All programming language has a build in data structure but with own changes and definition. There is some common data type like Integer, Strings, Boolean, Arrays, etc. Not a every language declare varible data types the same way.

JavaScript Data Types and Examples

In other languages, Data Type is used for define a variable type. Means what kind of value will store in this varible. A data type is a basic fundamental of any programming languages.

Where in JavaScript variable need to assign the value not a type before name it like in java. For an example like that -> int x = 19.

JavaScript Data Types

Let’s make it easy by dividing a data type into groups. JavaScript variable can be Statically Typed, dynamically typed and weakly typed.

Then come to other programming language segregation is mostly like that data type- Primitives and Object type. There is always an argument over the JS is not have any data type (untyped)

JavaScript Data Types

Dynamically Typed

In JavaScript Dynamic typing, variable type decide by its value after code run the code at compiler/interpreter. Means if a value of varible integer then variable will be an integer and if its strings then the variable type is also strings. But varible type can change later when a different type of data type value assigned to it. See below code how it will define.

Statically Typed

JavaScript DataTypes are not Statically Typed unless you using a tool Typescript or Flow that compiles to JS code. A Static type means to define a varible with its type. This varible is not easily changeable. Let’s see how to define that.

Weakly Typed

A JavaScript is Weakly typed languages. Which mean it allows one data types to be inferred as another type. See below example of it.

Where trying to add a number with a string  and result will be not addition. See the output.

Complete Example

Output:

 DataTypes in javascript

Primitives Data types in JavaScript

There are 6 Primitive data types in programming languages as same for JS. This is not an object, no method and immutable.

TypeValue
Booleantrue or false
NullNo Value
Undefined A Declared variable without value.
Numberintegers, floats, Double, Long etc.
Stringtext (written inside double or single quotes)
Symbol A unique value(new in ECMAScript 6)

Note: If the variable value is number in quotes, it will be treated as a text string.

Object type DataTypes in javascript

2 Main object are in JavaScript.

  • Object
  • Array

Many other objects, don’t confuse with primitive types. For below list act as constructors to create those types. For example Boolean('c') // true.

  • Function
  • Boolean
  • Symbol
  • Error
  • Number
  • String
  • RegExp
  • Math
  • Set
  • Date

For example of Object type and Primitive type will see on next tutorial.

Do comment if you have any doubt and suggestion of this article.

Note: The JavaScript DataTypes Examples are tested on Safari browser (Version 12.0.2).
OS: macOS 10.14 Mojave
Version: HECMAScript 2018


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.