Data types

All data used in JavaScript has a specific type. JavaScript has eight data types:

  • String : represents a string
  • Number : represents a numeric value
  • BigInt : designed to represent very large integers
  • Boolean : represents a Boolean true or false
  • Undefined : represents one special value – undefined and indicates that the value has not been set
  • Null : represents one special value – null and indicates the absence of a value
  • Symbol : represents a unique value that is often used to access the properties of complex objects
  • Object : represents a complex object

The first seven types represent primitive data types. The last type, Object , represents a complex, complex data type that consists of values ​​of primitive types or other objects. Consider the basic primitive data types.

Numeric data

number

The Number type represents numbers in JavaScript, which can be integers or fractions:

Integers such as 35. We can use both positive and negative numbers. Range of used numbers: -2 53 to 2 53

Fractional numbers (floating point numbers), such as 3.5575. Again, both positive and negative numbers can be used. Floating point numbers use the same range: -253 to 253

For example:

let x = 45;
let y = 23.897;

As a separator between integer and fractional parts, as in other programming languages, a period is used.

BigInt type

The BigInt type is added in recent JavaScript standards to represent very large integers that are out of range of the number type. This doesn’t mean that we can’t work with large numbers at all with the number type, but working with them in the case of the number type will be fraught with problems. Consider a small example:

let num = 9007199254740991
console.log(num);       // 9007199254740991
console.log(num + 1);   // 9007199254740992
console.log(num + 2);   // 9007199254740992

Here the variable num is assigned the maximum value. And then we add some values ​​\u200b\u200bto it and display the result on the console. And the results can confuse us, especially in the case of adding the number 2.

To define a number as a value of a type BigInt, the suffix n is added to the end of the number :

let dimension = 19007n;
const value = 2545n;

For example, let’s change the number type from the previous example to bigint:

let num = 9007199254740991n
console.log(num);       // 9007199254740991n
console.log(num + 1n);  // 9007199254740992n
console.log(num + 2n);  // 9007199254740993n
console.log(num + 3n);  // 9007199254740994n

Boolean type

The Boolean type represents the boolean or boolean values ​​true (true) and false (false):

let isAlive = true;
let isDead = false;

String

The type String represents strings. Quotes are used to define strings, and you can use both double, single, and slash quotes. The only restriction is that the type of the closing quote must be the same as the type of the opening quote, i.e. either both double or both single.

 
let user = "Tom";
let company = 'Microsoft';
let language = `JavaScript`;

console.log(user);
console.log(company);
console.log(language);

If there are quotes inside the string, then we must escape them with a slash. For example, let’s say we have text “Skyhound Internet”. Now we escape the quotes:

let company = "Skyhound Internet"";

We can also use another type of quotes inside the string:

let company1 = "Skyhound Internet";
let company2 = 'Easy WP Tutorial"';

Interpolation

The use of forward quotation marks allows us to apply a technique such as interpolation – to embed data in a string. For example:

 
let user = "Tom";
let text = `Name: ${user}`;
console.log(text);  // Name: Tom

To embed expression values ​​(for example, the values ​​of other variables and constants) in a string, precede the expression with a dollar sign $ , followed by the expression in curly braces. So, in the example above ${user}, it means that at this point in the line you need to embed the value of the variable user.

Similarly, you can embed more data:

 
let user = "Tom";
let age = 37;
let isMarried = false;
let text = `Name: ${user}   Age: ${age}   IsMarried: ${isMarried}`;
console.log(text);  // Name: Tom   Age: 37   IsMarried: false

null and undefined

undefined indicates that the value is not defined or not set. For example, when we just define a variable without assigning an initial value to it, it represents the undefined type:

 
let isAlive;
console.log(isAlive); // will output undefined

Assigning null means that the variable has no value:

let isAlive;
console.log(isAlive); // undefined
isAlive = null;
console.log(isAlive); // null
isAlive = undefined;  // again set the undefined type
console.log(isAlive); // undefined

object

The type objectrepresents a complex object. The simplest definition of an object is curly braces:

let user = {};

An object can have various properties and methods:

const user = {name: "Tom", age:24};
console.log(user.name);

In this case, the object is called user, and it has two properties: name and age. This is a brief description of objects, we will consider objects in more detail in subsequent articles.

Weak typing

JavaScript is a loosely typed language. This means that variables can change type dynamically. For example:

 
let id;  // type undefined
console.log(id);
id = 45;  // type number
console.log(id);
id = "45"; // type string
console.log(id);

Despite the fact that in the second and third cases the console will display the number 45, in the second case the variable idwill represent a number, and in the third case it will represent a string.

This is an important point that must be taken into account and on which the behavior of the variable in the program depends:

 
let a = 45;   
let b = a + 5;
console.log(b); // 50
    
a = "45";  
let c = a + 5
console.log(c); // 455

Above, in both cases , the aaddition (+) operation is applied to the variable. But in the first case it arepresents a number, so the result of the operation a + 5will be the number 50.

In the second case, it arepresents a string. But the addition operation between the string and the number 5 is not possible. Therefore, the number 5 will be converted to a string, and the string concatenation operation will take place. Therefore, in the second case, the result of the expression a + 5will be the drain “455”.

Typeof Operator

Using the typeof operator , you can get the type of a variable:

let id;
console.log(typeof id); // undefined

id = 45;
console.log(typeof id); // number

id = 45n;
console.log(typeof id); // bigint

id="45";
console.log(typeof id); // string

It’s worth noting that for null , the operator typeof returns “object”, even though according to the JavaScript specification, null represents a distinct type.