DEV Community

vedanth bora
vedanth bora

Posted on

Data types in Javascript

A value is Javascript will always be of some data type. It could be a string, a number, an object etc.

In this blog I will talk about the data types present in javascript, so by the end of this blog you should know all the data types in javascript.

There are 8 basic data types in JavaScript.

  • number for numbers of any kind: integer or floating-point, integers are limited by ±(2531).
  • bigint is for integer numbers of arbitrary length.
  • string for strings. A string may have zero or more characters, there’s no separate single-character type.
  • boolean for true/false.
  • null for unknown values – a standalone type that has a single value null.
  • undefined for unassigned values – a standalone type that has a single value undefined.
  • object for more complex data structures.
  • symbol for unique identifiers.

The typeof operator allows us to see which type is stored in a variable.

  • Usually used as typeof x, but typeof(x) is also possible.
  • Returns a string with the name of the type, like "string".
  • For null returns "object" – this is an error in the language, it’s not actually an object.

Lets look at each of them and understand what they are...

Number

The number type represents both integer and floating point numbers (A floating point number, is a positive or negative whole number with a decimal point)

We can perform many operations on numbers like addition + , subtraction - , multiplication * and more

Besides regular numbers, the number data type also has so-called “special numeric values” which also belong to this data type:

  • Infinity, represents the mathematical Infinity ∞. It is a special value that’s greater than any number. (We can get it as a result of division by zero, Or just reference it directly)
  • -Infinity
  • NaN. represents a computational error. It is a result of an incorrect or an undefined mathematical operation, (Any further mathematical operation on NaN returns NaN)
alert( 1 / 0 ); // Infinity
alert( Infinity ); // Infinity

alert( NaN + 5 ); // NaN
alert( 8 * NaN ); // NaN
alert( "not a number" / 6 - 4 ); // NaN
Enter fullscreen mode Exit fullscreen mode

Doing maths is “safe” in JavaScript. We can do anything: divide by zero, treat non-numeric strings as numbers, etc.

The script will never stop with a fatal error. At worst, we’ll get NaN as the result.


BigInt

In JavaScript, the “number” type cannot represent integer values larger than (253-1) (that’s 9007199254740991), or less than -(253-1) for negatives. It’s a technical limitation caused by their internal representation.

For most purposes that’s enough, but sometimes we need really big numbers, e.g. for cryptography or microsecond-precision timestamps.

To solve this the BigInt type was recently added to the language to represent integers of arbitrary length.

A BigInt value is created by appending n to the end of an integer:

// the "n" at the end means it's a BigInt
const bigInt = 12455345678901544454524543456766644454545678901234567890n;
Enter fullscreen mode Exit fullscreen mode

String

A string may consist of zero characters (be empty), one character or many of them. A string in JavaScript must be surrounded by quotes.

In JavaScript, there are 3 types of quotes.

  1. Double quotes: "Hello".
  2. Single quotes: 'Hello'.
  3. Backticks: Hello.

Double and single quotes are “simple” quotes. There’s practically no difference between them in JavaScript.

Backticks are “extended functionality” quotes. They allow us to embed variables and expressions into a string by wrapping them in ${…},

let name = "Ved";

// embed a variable
alert( `Hello, ${name}!` ); // Hello, Ved!

// embed an expression
alert( `the result is ${5 + 2}` ); // the result is 7
Enter fullscreen mode Exit fullscreen mode

The expression inside ${…} is evaluated and the result becomes a part of the string. We can put anything in there: a variable like name or an arithmetical expression like 6 + 2 or something more complex.

Please note that this can only be done in **backticks**.


Boolean

The boolean type has only two values: true and false. It is also known as the logical type as this is commonly used to store yes/no values: true means “yes, correct”, and false means “no, incorrect”.

let listItemOneChecked = true; // yes, listItemOne  is checked
let listItemTwoChecked = false; // no, listItemTwo  is not checked

let isGreaterOne = 7 > 1;
let isGreaterTwo = 7 > 8;

alert( isGreaterOne ); // true (the comparison result is "yes")
alert( isGreaterTwo ); // false (the comparison result is "yes")
Enter fullscreen mode Exit fullscreen mode

Null

In JavaScript, null is not a “reference to a non-existing object” or a “null pointer” like in some other languages. It forms a separate type of its own which contains only the null value:

It’s just a special value which represents “nothing”, “empty” or “value unknown”.

let age = null;
Enter fullscreen mode Exit fullscreen mode

The code above states that age is unknown.


undefined

Javascript also have a special value undefined . It makes a type of its own, just like null. The meaning of undefined is “value is not assigned”. If a variable is declared, but not assigned, then its value is undefined:

let age = 100;

// change the value to undefined
age = undefined;

alert(age); // "undefined"
Enter fullscreen mode Exit fullscreen mode

Technically, it is possible to explicitly assign undefined to a variable:


Objects and Symbols

-

The object type is special as all other types are called “primitive” because their values can contain only a single thing (be it a string or a number or whatever). But, objects are used to store collections of data and more complex entities.

let user = new Object(); // "object constructor" syntax
let user = {};  // "object literal" syntax
Enter fullscreen mode Exit fullscreen mode

Symbol

The symbol type is used to create unique identifiers for objects. A “symbol” represents a unique identifier. A value of this type can be created using Symbol():

let id1 = Symbol("id");
let id2 = Symbol("id");

alert(id1 == id2); // false
Enter fullscreen mode Exit fullscreen mode

typeof

The typeof operator returns the type of the argument.

A call to typeof x returns a string with the type name:

typeof undefined // "undefined"

typeof 0 // "number"

typeof 10n // "bigint"

typeof true // "boolean"

typeof "foo" // "string"

typeof Symbol("id") // "symbol"

typeof Math  // "object"  (1)
typeof null  // "object"  (2)
typeof alert // "function"  (3)
Enter fullscreen mode Exit fullscreen mode

The last three lines may need additional explanation:

  1. Math is a built-in object that provides mathematical operations.
  2. The result of typeof null is "object". The behavior of typeof is wrong here. That’s an officially recognized error in typeof, coming from very early days of JavaScript and kept for compatibility. Definitely, null is not an object. It is a special value with a separate type of its own.
  3. The result of typeof alert is "function", because alert is a function. That also comes from the early days of JavaScript. Technically, such behavior isn’t correct, but can be convenient in practice. There’s no special “function” type in JavaScript. Functions belong to the object type. But typeof treats them differently, returning "function".

To put it clear: typeof is an operator, not a function.


If you’ve managed to read till here, do drop a like 😆

Top comments (0)