JavaScript has primitives, objects, and functions. All of them are values. All are treated as objects, even primitives.
Number, boolean, string, undefined
and null
are primitives.
There is only one number type in JavaScript, the 64-bit binary floating point type. Decimal numbers’ arithmetic is inexact.
As you may already know, 0.1 + 0.2
does not make 0.3
. But with integers, the arithmetic is exact, so 1+2 === 3
.
Numbers inherit methods from the Number.prototype
object. Methods can be called on numbers:
(123).toString(); //"123"
(1.23).toFixed(1); //"1.2"
There are functions for converting strings to numbers : Number.parseInt()
, Number.parseFloat()
and Number()
:
…
This short post shows how to check the size of an array and verify if it is not empty.
The length
property of an array gives the number of elements in that array. To check if an array is not empty we simply test if the length property is not 0
.
const arr = [];if(arr.length !== 0) {
console.log('empty')
}
0
, null
, undefined
are falsy values, which means they are evaluated to false
in a boolean context like the condition for the if
statement. Here is another way to check if an array is not empty.
const arr =…
In this article, we will take a look at how to sort arrays of numbers, strings, and objects. In the end, we will try to create a generic function that can sort a list of data objects by any number of properties.
The sort
method converts elements to strings and sorts them alphabetically. This, of course, works great for sorting strings.
const fruits = ["Lemon", "Apple", "Mango"];
fruits.sort()console.log(fruits);
//["Apple", "Lemon", "Mango"]
Remember that the sort
method is impure. It changes the input array.
As said sort
converts values to strings before sorting them. This serves us well for sorting…
It may happen that you need to convert an array of objects into map objects or vice versa. In this post, we will look at how to do that.
Consider we have an array of game objects. Each game object has an id
and a name
.
const games = [
{id:1, name: 'Fornite'},
{id:2, name: 'RDR2'},
{id:3, name: 'Cyberpunk 2077'}
];
We want to convert the array of game data into an object mapping the id
to the name
of the game.
{
1: "Fornite",
2: "RDR2",
3: "Cyberpunk 2077"
}
One solution is to use the forEach
method to…
First of all, we should understand what an object actually is in this language.
An object is a dynamic collection of properties with a hidden property to its prototype.
This definition may look different to you since an object in other languages is an instance of a class. In JavaScript, it is just a collection of key-value pairs.
Once an object is created, we can add, edit, or delete its props. Consider the following empty object:
const game = {};
game.name = 'Candy Crush'; //prop added
game.name = 'Candy Crush Saga'; //prop edited
delete game.name //prop deleted
Objects indeed have…
Objects and maps are both dynamic collections of key-value pairs.
Starting from this definition they are pretty similar but there are some differences between them we are going to find out.
The object literal syntax is the simplest way to create an object map in JavaScript.
const gamesObj = {
1: 'Citadels',
2: 'Tzolkin'
};
Maps are created using the built-in Map
constructor.
const gamesMap = new Map([
[1, 'Citadels'],
[2, 'Tzolkin']
]);
From now on I am going to use the words “object” or “object maps” for key-value collections created using the object literal syntax and the word “maps”…
Functions are one of the most important concepts in JavaScript. In this article, we will look at the essential things you may need to know about them.
function compute(){}
When using the function declaration, the function
keyword is the first on the line. The function must have a name and can be used before definition.
Function declarations are moved, or “hoisted,” to the top of their scope. This means we can use a function before it is declared. The following example is a valid usage of a function created with the function declaration:
compute();function compute(){}
const compute = function()…
In this article, we will look at what I think are some of the most important — and unique — features of JavaScript.
Functions are units of behavior, but the important part here is that they are independent. In other languages like Java or C#, functions must be declared inside a class. This is not the case in JavaScript.
Functions can be declared in the global scope or defined inside a module as independent units that can be reused.
Objects are indeed just a collection of properties. In other languages, they are called maps, hash maps, or hash tables.
They…
In this article, we will look at different ways the built-in Number
function can be used in JavaScript. Basically, this is helpful for working with numbers.
Number
can convert other value types to numbers. The most common scenario is the conversion of strings to numbers using the Number
function.
console.log(Number('1'));
//1
console.log(Number('1.2'));
//1.2
console.log(Number(''));
//0
console.log(Number(' 1 '));
//1
It can convert also non-string types to numbers, but the result may be unexpected.
console.log(Number(true));
//1
console.log(Number(false));
//0
1console.log(Number([]));
//0
console.log(Number([1]));
//1
console.log(Number([1.5]));
//1.5
A more practical conversion will be to convert dates to numbers. …
There are cases when we want to create an array filled with default values. For small arrays, of course, we can just write down the values when declaring the array but for larger arrays, a nicer system may be needed.
const arr = [false, false, false, false, false];
Let’s try to do the previous initialization in a nicer way.
The Array
built-in function can be used to create an array with the specified length but with no actual values in it.
Array(5)
gives an array of length 5
but with no values.
const arr = Array(5);
console.log(arr.length);
//5
The fill()
…
Author of Functional JavaScript book series. Enthusiastic about sharing ideas. https://www.amazon.com/gp/product/B08X3TPCQ8