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

Woman blowing confetti
Woman blowing confetti

In this article, we will look at what I think are some of the most important — and unique — features of JavaScript.

1. Functions Are Independent Units of Behavior

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.

2. Objects Are Dynamic Collections of Props

Objects are indeed just a collection of properties. In other languages, they are called maps, hash maps, or hash tables.

They…


Get up to speed with the Number, String, and Boolean functions

Boats docked on a pier
Boats docked on a pier

This article takes a look at how to convert between the primitive values in JavaScript. Even if automatic type coercion is sometimes promoted as a way of doing type conversion “like a pro” or as being shorter and as such better, I consider the built-in String, Number, and Boolean functions to be better options.

Converting to Numbers

The automatic type conversion system, also called type coercion in JavaScript, converts values to numbers when using the - and + operators before a single value.

Here is the “one-liner” that is sometimes promoted as the smart way of converting strings to numbers:

+"1" //1typeof…


Quick Start with the Math.random and Math.floor functions

In this article, we will look at how to get a random value from an array. Basically, we are going to use the Math.random index to generate a random index and then return the element.

Let’s start.

Math.random

The Math.random utility returns a random number between 0 and 1. The random number is greater or equal to 0 but less than 1.

Here is an example.

console.log(Math.random());
//0.6168018976827372

Random Number in a Range

Before looking at how to generate a random index, we need to be able to generate a random integer number between 0 and a maximum number.

Math.random returns a number less than 1…


Prototypes, Object.freeze, Object.create and more

Have you ever wondered what happens when freezing the prototype of an object? Let’s find out together.

Objects

In JavaScript, objects are dynamic collections. Here is such an object created using the object literal syntax.

const counter = {
count: 0,

increment(){
this.count += 1;
return this.count;
},

decrement(){
this.count -= 1;
return this.count
}
}
counter.increment();
//1

counter is an object with a field and two methods operating on it.

Prototypes

Objects can inherit properties from other objects called prototypes. They have a “hidden” property called __proto__ pointing to their prototype. As a matter of fact, the counter object already inherits…


Freezing objects, getting all keys and values, inheriting from prototypes, and more

This article will look at perhaps the most important utility functions for working with objects in JavaScript.

Object.freeze

Object.freeze() freezes an object. A frozen object cannot be changed anymore. It returns the same object that was passed in.

This is the simplest way to achieve immutability in JavaScript by freezing the object at creation.

const game = Object.freeze({
name: 'Warcraft'
});
game.developer = 'Blizzard';
//TypeError: Cannot add property developer, object is not extensible

The only thing to note is that Object.freeze() freezes only the immediate properties of an object doing what is called a “shallow freeze”. …


Put in practice the String and Number built-in functions, the split and reduce methods

This post shows a different solution to the problem of computing the sum of the digits in a number.

The first step is to convert the number to an array of digits. To achieve this we first convert it to a string, then convert the string into an array of chars. In the end, the array of number characters is converted into an array of numbers.

The String built-in function can transform any value into a string. Below is an example of transforming a number into a string.

String(12345)
//"12345"

The split method splits a text into an array of…


Quick start with maps, pipelines, and the reduce method

In this post, we will take a look at how to count the number of occurrences of each letter in a word with both JavaScript and Elixir. In both cases, we are going to create a map with the result.

The algorithm will be pretty similar:

  • split the text into an array of characters
  • use the reduce method over the array to compute the counts

Here are the letter counts for the Hello word.

// { H: 1, e: 1, l: 2, o: 1 }
# %{"H" => 1, "e" => 1, "l" => 2, "o" => 1}

Count characters in JavaScript

We can…


Fixing the inexact decimal arithmetic by using integer arithmetic

Adding two numbers should be a simple operation, and in many cases it is.

1 + 2
//3

However, sometimes we get into unexpected issues because the plus operator (+) is both the addition and concatenation operator. When one of the operands is a string it becomes the concatenation operator.

1 + '2'
//'12'

In such cases, we need to make sure both operands are numbers. The Number built-in function can convert any value into a number.

1 + Number('2')
//3

There are cases when we need to add decimal numbers are we are surprised by the result.

0.1 +…


NaN is a number, NaN is not equal to NaN, isNaN gives false positives and more

This article takes a deeper look at the NaN value read “Not-A-Number”.

1. NaN is a number

In JavaScript, invalid numbers are represented using the NaN value. NaN and Number.NaN both represent the same value.

Even if is called “Not-A-Number” is actually a number.
A better name would have been Not-A-Valid-Number.

typeof NaN
//number

2. NaN is not equal to NaN

NaN is not equal to itself. This can be a little strange. It is the only value in the language not equal to itself.

NaN === NaN
//false

That implies that a variable storing the NaN value is not equal to itself.

const x = NaN;x === x
//false

3. Invalid conversion results in NaN

The…


Focus, learning trips, keep a to-do list, and more

It was 96 when Quake came out and I first heard about John Carmack. It was impressive to discover that he did all the game engine by himself. And the game looked and played just great. Around 2012, I found and read for the first time the Masters of Doom book.

I was intrigued to see Carmack’s work ethic because no matter how smart you are, you need to put in a huge amount of work to build an engine as he did. His game engines were an incredible technology at the time, something never achieved before.

1. Put more working hours

I was surprised…

Cristian Salcescu

Author of Functional Programming in JavaScript. 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