JavaScript Data Types

JavaScript is a loosely or weakly typed language. Therefore, whatever the value assigned to a variable, the variable changed its typed based on the assigned value.

Or we can say, in JavaScript, there is no need to declare or define the type of variable before its use.

In JavaScript, here are the list of 5 data types based on values:

  1. string - Used to store string type value like "codescracker"
  2. number - Used to store number type value like 120
  3. boolean - Used to store boolean type value. The two boolean type values are true and false
  4. object - Used to store object type value like [1, 2, 3, 4, 5]
  5. function - Used to store function type value like function () {}

For example:

x = "codescracker";    // x is of string type
x = 120;               // now x is of number type
x = true;              // now x is of boolean type

See the above example, there is no need to declare the type of variable before initializing any value of that type.

The JavaScript typeof operator is used to find the type of value/variable. For example:

HTML with JavaScript Code
<!DOCTYPE html>
      document.write(typeof "codescracker", "<BR>");
      document.write(typeof 10, "<BR>");
      document.write(typeof true, "<BR>");
      document.write(typeof [1, 2, 3, 4, 5], "<BR>");
      document.write(typeof function () {}, "<BR>");

JavaScript allows three keyword that are: var, let, and const to declare a variable.

JavaScript Online Test

« Previous Tutorial Next Tutorial »