DEV Community

Bilal Niaz
Bilal Niaz

Posted on

Data type in javascript:

There are eight basic data types in JavaScript. We can put any type in a variable. For example, a variable can at one moment be a string and then store a number:

// no error
let message = "hello";
message = 123456
Enter fullscreen mode Exit fullscreen mode

1) Number
The number type represents both integer and floating point numbers.

let n = 123;
n = 12.345;
Enter fullscreen mode Exit fullscreen mode

Aside from conventional numbers, this data type also includes "special numeric values" such as Infinity, -Infinity, and NaN.
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:

alert( 1 / 0 ); // Infinity
Enter fullscreen mode Exit fullscreen mode

NaN represents a computational error. It is a result of an incorrect or an undefined mathematical operation, for instance

alert( "not a number" / 2 ); // NaN, such division is erroneous
Enter fullscreen mode Exit fullscreen mode

Special numeric values formally belong to the “number” type. Of course they are not numbers in the common sense of this word.

2) BigInt
The "number" type in JavaScript cannot express integer values greater than (2^53-1) (that is, 9007199254740991), or negative values less than -(2^53-1). It's a technological stumbling block resulting from their internal representation.
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 = 1234567890123456789012345678901234567890n;
Enter fullscreen mode Exit fullscreen mode

3) 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. In JavaScript, there are 3 types of quotes.
Double quotes.
Single quotes.
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 ${…}, for example:

et str = "Hello";//double quotes
let str2 = 'Single quotes are ok too';//single quotes
let phrase = `can embed another ${str}`;//Backticks
Enter fullscreen mode Exit fullscreen mode

4) Boolean (logical type)
Boolean represents a logical entity and can have two values: true and false .

let nameFieldChecked = true; // yes, name field is checked
let ageFieldChecked = false; // no, age field is not checked
Enter fullscreen mode Exit fullscreen mode

Boolean values also come as a result of comparisons:

let isGreater = 4 > 1;
console.log( isGreater ); // true (the comparison result is yes)
Enter fullscreen mode Exit fullscreen mode

5) null
The Null type has exactly one value: null.

let age = null;
Enter fullscreen mode Exit fullscreen mode

n JavaScript, null is not a “reference to a non-existing object” or a “null pointer” like in some other languages.

It’s just a special value which represents “nothing”, “empty” or “value unknown”.
6) undefined
A variable that has not been assigned a value has the value undefined.

let age;
console.log(age); // shows "undefined"
Enter fullscreen mode Exit fullscreen mode

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

let age = 100;
// change the value to undefined
age = undefined;
console.log(age); // "undefined"
Enter fullscreen mode Exit fullscreen mode

7) Object
In JavaScript, objects can be seen as a collection of properties. With the object literal syntax, a limited set of properties are initialized; then properties can be added and removed.An object can be created with figure brackets {…} with an optional list of properties. A property is a “key: value” pair, where key is a string (also called a “property name”), and value can be anything.

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

We can immediately put some properties into {...} as “key: value” pairs. The last property in the list may end with a comma:

let user = {     // an object
  name: "John",  // by key "name" store value "John"
  age: 30        // by key "age" store value 30
Enter fullscreen mode Exit fullscreen mode

Property values are accessible using the dot notation:

// get property values of the object:
console.log( ); // John
console.log( user.age ); // 30
Enter fullscreen mode Exit fullscreen mode

Adding a new value to an object is as follows: = "Lahore"
Enter fullscreen mode Exit fullscreen mode

To remove a property, we can use delete operator:

delete user.age;
Enter fullscreen mode Exit fullscreen mode

8) Symbol
A Symbol is a unique and immutable primitive value and may be used as the key of an Object property (see below). In some programming languages, Symbols are called "atoms".

// Here are two symbols with the same description:
let Sym1 = Symbol("Sym")
let Sym2 = Symbol("Sym")

console.log(Sym1 === Sym2) // returns "false"
// Symbols are guaranteed to be unique.
// Even if we create many symbols with the same description,
// they are different values.
Enter fullscreen mode Exit fullscreen mode

Symbol type is a new feature in ECMAScript 2015. There is no ECMAScript 5 equivalent for Symbol.

number for numbers of any kind: integer or floating-point, integers are limited by ±(253-1).
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
undefined for unassigned values
objectfor more complex data structures.
symbol for unique identifiers.
I hope you enjoy it.

Top comments (0)