JavaScript Introduction | Types | Data Type

Data Type

A data type means the kind of value that a program can handle.
JavaScript defines and provides several types in advance, and these are called basic types.
JavaScript basic types can be divided broadly into primitive types and object types.

Primitive types are as follows.

  1. Number
  2. String
  3. Boolean
  4. Symbol: provided from ECMAScript 6
  5. undefined

The object type is as follows.

  1. Object
var num = 10;          // number
var myName = "Hong Gil-dong"; // string
var str;               // undefined

Number

Unlike some other languages, JavaScript does not distinguish between integers and real numbers. It represents all numbers as one kind of real number. You can also use exponential notation to express very large or very small numbers.

var firstNum = 10;     // expression without a decimal point
var secondNum = 10.00; // expression with a decimal point
var thirdNum = 10e6;   // 10000000
var fourthNum = 10e-6; // 0.00001

String

In JavaScript, a string means a set of characters enclosed in double quotation marks ("") or single quotation marks (’’). Double quotation marks can be included only in a string enclosed in single quotation marks, and single quotation marks can be included only in a string enclosed in double quotation marks.

var firstStr = "This is also a string.";      // string using double quotation marks
var secondStr = 'This is also a string.';     // string using single quotation marks
var thirdStr = "My name is 'Hong Gil-dong'."  // single quotation marks can be included only in a string enclosed in double quotation marks.
var fourthStr = 'My name is "Hong Gil-dong".' // double quotation marks can be included only in a string enclosed in single quotation marks.

In JavaScript, you can add numbers and strings. In this case, JavaScript automatically converts the number to a string and performs string concatenation.

var num = 10;
var str = "JavaScript";
document.getElementById("result").innerHTML = (num + str); // 10JavaScript

Boolean

A boolean value represents true or false.

In JavaScript, boolean values are represented with the reserved words true and false.

var firstNum = 10;
var secondNum = 11;
document.getElementById("result").innerHTML = (firstNum == secondNum); // false

The following values are evaluated as false.

false
undefined
null
0
NaN
the empty string ("")

All other values, including all objects, are evaluated as true when passed to a conditional statement.

Symbol

The symbol type was newly added in ECMAScript 6.
A symbol is a unique and immutable type, and it can be used as an identifier for an object property.

var sym = Symbol("javascript");  // symbol type
var symObj = Object(sym);        // object type

The symbol type is not supported in Internet Explorer.

typeof Operator

The typeof operator is an operator with only one operand that returns the type of that operand.

typeof 10;        // number type
typeof "string";  // string type
typeof true;      // boolean type
typeof undefined; // undefined type
typeof null;      // object type

null and undefined

In JavaScript, null is an object type and means that a value has not yet been determined.
Unlike null, undefined means that the type has not been determined.
Therefore, in JavaScript, undefined is returned when you access an uninitialized variable or a value that does not exist.

var num;          // returns undefined because it is not initialized.
var str = null;   // null value of the object type
typeof secondNum; // returns undefined when accessing an undefined variable.

Be careful because null and undefined produce different results when compared with the equality operator (==) and the strict equality operator (===).
null and undefined have the same meaning except for their type, but they are not strictly equal because their types differ.

null ==  undefined; // true
null === undefined; // false

Object

JavaScript’s basic type is the object. An object can be understood as a thing that we can recognize in real life. An object is a kind of collection that groups several properties or methods under the same name.

var dog = { name: "Happy", age: 3 }; // create an object
// Reference object properties
document.getElementById("result").innerHTML =
    "The dog's name is " + dog.name + ", and it is " + dog.age + " years old.";