DEV Community

Kaushal
Kaushal

Posted on

JavaScript data-types

Introduction

  • Inside the computer’s world, there is only data which define in bits.
  • All this data is stored as long sequences of bits and is thus fundamentally alike.
  • Bits are any kind of two-valued things, usually described as zeros and ones.

For example, we can express the number 13 in bits. It works the same way as a decimal number, but instead of 10 different digits, you have only 2, and the weight of each increases by a factor of 2 from right to left. Here are the bits that make up the number 13, with the weights of the digits shown below them:

128 64 32 16 8 4 2 1
  0  0  0  0 1 1 0 1

So that’s the binary number 00001101. Its non-zero digits stand for 8, 4, and 1, and add up to 13.

Dynamic typing

  • JavaScript is a loosely typed and dynamic language.
  • Variables in JavaScript are not directly associated with any particular value type, and any variable can be assigned (and re-assigned) values of all types:
let foo = 42;    // foo is a number
foo     = 'bar'; // foo is now a string
foo     = true;  // foo is now a boolean
  • primitive (or primary): These types are called “primitive” because their values can contain only a single thing (be it a string or a number or whatever).
    • String
    • Number
    • BigInt
    • Boolean
    • Undefined
    • Null
  • composite (or reference)
    • Object
    • Function

Primitive

String, Number, and Boolean are primitive data types.

String

  • JavaScript's String type is used to represent textual data. It is a set of "elements" of 16-bit unsigned integer values. Each element in the String occupies a position in the String. The first element is at index 0, the next at index 1, and so on. The length of a String is the number of elements in it.
  • We can use single quotes, double quotes, or backticks to mark strings, as long as the quotes at the start and the end of the string match.
  • For instance,
var a = "Welcome to Devkode";
var b = 'LEARN | DEVELOP | DEPLOY';
var c = `KEEP LEARNING KEEP SHARING`;

console.log(a); // Welcome to Devkode
console.log(b); // LEARN | DEVELOP | DEPLOY
console.log(c); // KEEP LEARNING KEEP SHARING
  • Unlike C language JavaScript's strings are immutable. This means a string created, it is not possible to modify it.

Number

  • JavaScript automatically takes data-type on what value are you assigning.
  • For instance,
var a = 10;
console.log(a) // 10
  • JavaScript store value between -(253 − 1) and 253 − 1).
  • The number typehas three symbolic value +Infinity, -Infinity, and NaN ("Not a Number").
  • We can also check minmum value and maximum value within ±Infinity.
console.log(Number.MAX_VALUE); // 1.7976931348623157e+308
console.log(Number.MIN_VALUE); // 5e-324
  • In ECMAScript2015, we can albe to check the number is in the double-precision floating-point number range using Number.isSafeInteger(). We can get the rang of SafeInteger using Number.MAX_SAFE_INTEGER and Number.MIN_SAFE_INTEGER. Beyond this range, interget is not safe in JavaScript

BigInt

  • The BigInt type is a numeric primitive in JavaScript that can represent integers with arbitrary precision.
  • For assign BigInt value to variable, we have to use n character followed by integer value.
var a = 5n;
console.log(a); // 5n
  • With the BigInt, we can store a value beyond the safe number limit for Number.
  • BigInt is not strictly equal to a Number, but it is loosely so.

Boolean

  • It is often useful to have a value that distinguishes between only two possibilities, like yes and no or on and off.
  • Boolean has just two values, true and false, which are writtern as word.
var a = false;
var b = true;
console.log(a) // false
console.log(b) // true

Null

  • The special null value does not belong to any of the types described above.
  • In JavaScript, null is not a “reference to a non-existing object” or a “null pointer” like in some other languages.
var a = null;
console.log(a) // null

Undefined

  • The special value undefined also stands apart. It makes a type of its own, just like null.

  • The meaning of undefined is “value is not assigned” to specified variable.

var a;
console.log(a); // undefined

Composite

Object, Array, and Function (which are all types of objects) are composite data types.

Object

  • Objects allow us to group values—including other objects—to build more complex structures.
  • Objects are used to store collections of data and more complex entities.
  • An object comes with a pair of keys and values.
var a = { name: 'value' };
console.log(a); // {name: "value"}
  • We can also create an object instance with the help of a new keyword. This way we can create an object instance of a class
var str = new String();
var bool = new Boolean();
var num = new Number();

Function

  • Functions are regular objects with the additional capability of being callable.
console.log(console.log) // ƒ log() { [native code] }

Special

  • Symbols are new to JavaScript in ECMAScript 2015.
  • A Symbol is a unique and immutable primitive value and may be used as the key of an Object property.
  • In some programming languages, Symbols are called "atoms".

Summary

The typeof operator returns the type of the argument. It’s useful when we want to process values of different types differently or just want to do a quick check.

typeof 0 // "number"
typeof 10n // "bigint"
typeof "foo" // "string"
typeof true // "boolean"
typeof null // "object"
typeof undefined // "undefined"
typeof Math // "object"
typeof Symbol("id") // "symbol"
typeof alert // "function"
  • number for any kind of numbers of any kind: integer or floating-point, integers are limited by ±253.
  • bigint is for integer numbers of arbitrary length.
  • string for strings. A string may have zero or more characters.
  • boolean for true/false.
  • null for unknown values.
  • undefined for unassigned values.
  • object for more complex data structures.
  • symbol for unique identifiers.

Top comments (0)