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 value. That why it’s called Dynamic typing.

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

JavaScript Data Types and Examples

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

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 variables 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 runs the code at compiler/interpreter. This 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.

var a = 10; // int
var b = 'test'; // string

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.

int x = 99;
string y = 'abc';

Weakly Typed

JavaScript is a Weakly typed language. This means it allows one data type to be inferred as another type. See below example of it.

var a = 2 + '4'; // '24'

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

Complete Example

<html>
    <head>
        <script>
            function myFunction() {
                var number = 2 + '4';
                document.getElementById("myText").innerHTML = number;
            }
        </script>
    </head>
    <body onload="myFunction()">
        
        <p>the value for number is: <span id="myText"></span></p>
        
    </body>
</html>

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.