JavaScript Data Types



 Data type refers to what type of data (value) is stored in variable .

In JavaScript, there are two types of data types:

The first is primitive data types. The basic data types include:

  • Boolean : Contains only two values true/false
  • null: null is a special value (keyword), indicating that there is nothing in this variable
  • undefined: undefined is also a special value (keyword), meaning that the value has not been defined or specified
  • Number (Number) : Value of numeric type, such as 42 / 3.14159 / 0
  • String : like'hello world' / car
  • Symbol

The second type is composite data types, which includes:

  • Array : The array is used to store multiple data. The number of data in the array is the length of the array (length)
  • Object : Basically, everything except the basic data type is an object type

Why is there a data type? Let’s first think about it if the data has no format, the following example computer will not know how to deal with it:

1 + 2
// Should it be regarded as the sum of numbers equal to 3?
// Or does the sum of strings equal 12?

Only with the data type can we know what type of data value belongs to and which operations are supported.

JavaScript is a dynamically typed language (Dynamically Typed Language)

JavaScript is a dynamically typed language. When you declare a variable, you don't need to assign a type to the variable.

// For example, you don’t need to tell the JS interpreter that score is a number like this
int score = 101;

The JavaScript Interpreter (Browser) will automatically give an appropriate type based on the value you give.

// So just declare the variable directly and give the value like this
var score = 101;

The dynamic type also means that you can specify a different type of value at any time to give the same variable:

var score = 101; // Numerical type
score = 'Mike gets 101 points'; // String type

typeof

The typeof operator is used to determine the data type of an operand (operand).

typeof operand

E.g:

// Output string
console.log(typeof 'hello');
// Output number
console.log(typeof 123);
// Output boolean
console.log(typeof true);

Post a Comment

0 Comments