JavaScript data types. Examples open

JavaScript data types. Examples

Approved. Code works!
This is exactly the working code that is verified by the moderator or site administrators

All data used in JavaScript has a certain type. There are several such types in JavaScript.

To find out the type of data contained in a variable, you can use the typeof operator:

console.log(typeof number)

Type Number

Represents numbers. Numbers can be integers or fractional, positive and negative:

var number = 45;//integers can have positive values
var number = -45;//and negative
!Range of numbers used: from – 2 to the 53rd power to 2 to the 53rd power
var number = 4.55;//floating point fractional numbers
var number = -4.55;//they can also be negative

Type BigInt

The BigInt type is added in the latest JavaScript standards to represent very large integers, which are outside the range of type number. This does not mean that we cannot work with large numbers at all with using the Number type, but working with them in the case of the number type will be fraught with problems.

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

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

Type Boolean

The Boolean type represents the boolean values true and false:

let isTrue = true;
let isFalse = false;

Type String

The String type represents strings. Quotes are used to define strings.

let user = "Alex";
let city = 'Santiago';

If there are quotes inside the string, then we must escape them with a slash:

let band = "my band name is Called \"Exolots\"";

To avoid escaping inside the parentheses, we can use another type of quotes:

let band = 'my band name is Called "Exolots"';

Interpolation

Interpolation – embedding data in a string. For example:

let username = "Alex";
let about = `Name: ${username}`;
console.log(about); // Name: Alex

Type Underfined

Type Undefined indicates that the value is not set or undefined. For example, when we allocated variable but did not assign a value to it:

let testUnderfined;
console.log(testUnderfined);//returns underfined

Type Null

Assigning the Null type means that the variable has no value:

testNull = null;
console.log(testNull);//returns null

Type object

The type Object represents a complex data object. The simplest definition of an object is braces:

let person = {};

An object can have various properties and methods:

const user = { name: "Tom", age: 24 };

Symbol

The symbol data type in JavaScript represents unique and immutable values that can be used as identifiers for properties of objects. Each symbol value is unique, and no two symbols can be equal to each other.

//Creation
const mySymbol = Symbol();

//Using a symbol as an identifier for an object property
const obj = {
  [mySymbol]: 'Symbol value'
};

console.log(obj[mySymbol]); // Output 'Symbol value'

Array

The array data type in JavaScript represents an ordered collection of elements. Arrays in JavaScript can contain any type of data, including numbers, strings, objects, and other arrays.

//Create array
const myArray = [1, 2, 3, 'four', true];

//Accessing array elements
console.log(myArray[0]); //  1
console.log(myArray[3]); // four

//Changing array elements
myArray[1] = 'two';
console.log(myArray); // Return [1, 'two', 3, 'four', true]

//Adding an element to the end of an array
myArray.push('five');
console.log(myArray); //Return [1, 'two', 3, 'four', true, 'five']

//Array length
console.log(myArray.length); // 6

Function

The function data type in JavaScript is a block of code that can be called to perform specific actions. Functions in JavaScript can take arguments and return values.

//Function Declaration
function greet(name) {
  console.log(`Hey, ${name}!`);
}

//Calling
greet('Alex'); // Return "Hey, Alex!"

function add(a, b) {
  return a + b;
}

const result = add(3, 5);
console.log(result); //Return 8
Functions in JS. Code examples open
Functions in JS. Code examples
July 25, 2022
Roman-tk

Date

The Date data type in JavaScript represents a date and time. It allows you to work with dates, perform comparison operations, get and set various date components (year, month, day, hours, minutes, seconds, etc.).

//Creation object Date
const currentDate = new Date();

//Getting the current date and time
console.log(currentDate); //Displays the current date and time

// Получение компонентов даты
const year = currentDate.getFullYear();
const month = currentDate.getMonth() + 1; //Months in JavaScript are numbered starting from 0, so we add 1
const day = currentDate.getDate();
const hours = currentDate.getHours();
const minutes = currentDate.getMinutes();
const seconds = currentDate.getSeconds();

console.log(`${day}.${month}.${year}`);
console.log(`${hours}:${minutes}:${seconds}`); 

Dynamic Typing in Javascript

JavaScript is a weakly typed language, which means that variables can dynamically replace their type.

Consider an example:

let a = 25; // type number
let b = a + 5;
console.log(b); // 30

d = "25"; // type string
let c = d + 5
console.log(c); // 255

We get “255“, it happens because the number 5 will be converted to string. String concatenation will take place.

0

More

Leave a Reply

Your email address will not be published. Required fields are marked *

How many?: 22 + 22

lil-code© | 2022 - 2024
Go Top
Authorization
*
*
Registration
*
*
*
*
Password generation