Primitives, variables, objects, arrays, dynamic typing, and more

Primitives

Number

(123).toString();  //"123"
(1.23).toFixed(1); //"1.2"


Learn about the length property, falsy values, and optional chaining operator

const arr = [];if(arr.length !== 0) {
console.log('empty')
}
const arr =…


Sorting lists of strings, numbers, and objects. Building a generic compare function.

Sorting Arrays of Strings

const fruits = ["Lemon", "Apple", "Mango"];
fruits.sort()
console.log(fruits);
//["Apple", "Lemon", "Mango"]

Sorting Arrays of Numbers


Transforming a List into a Map Object

const games = [
{id:1, name: 'Fornite'},
{id:2, name: 'RDR2'},
{id:3, name: 'Cyberpunk 2077'}
];
{
1: "Fornite",
2: "RDR2",
3: "Cyberpunk 2077"
}

forEach


You may have heard this, but let’s see if that really is the case

Tall buildings from street level
Tall buildings from street level
Photo by Boris Stefanik on Unsplash.

Objects

const game = {};
game.name = 'Candy Crush'; //prop added
game.name = 'Candy Crush Saga'; //prop edited
delete game.name //prop deleted


Construction, types and order of keys, JSON support, and more

1. The construction is of course different

const gamesObj = {
1: 'Citadels',
2: 'Tzolkin'
};
const gamesMap = new Map([
[1, 'Citadels'],
[2, 'Tzolkin']
]);


Auto-execute after the definition, functions can be closures, and more

A disco ball.
A disco ball.
Photo by Girl with red hat on Unsplash

1. Functions Can Be Declared Using the Function Declaration

function compute(){}
compute();function compute(){}

2. Functions Can Be Created Using Function Expressions

const compute = function()…


Functions are values, objects inherit from other objects, and more

Woman blowing confetti
Woman blowing confetti
Photo by Erik Brolin on Unsplash.

1. Functions Are Independent Units of Behavior

2. Objects Are Dynamic Collections of Props


Converting to Numbers

console.log(Number('1'));
//1
console.log(Number('1.2'));
//1.2
console.log(Number(''));
//0
console.log(Number(' 1 '));
//1
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


const arr = [false, false, false, false, false];

Initialize with Primitives

Array(length)

const arr = Array(5);
console.log(arr.length);
//5

fill(defaultValue)

Cristian Salcescu

Author of Functional JavaScript book series. Enthusiastic about sharing ideas. https://www.amazon.com/gp/product/B08X3TPCQ8

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store