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.


How to do function overloading using the typeof operator

If you worked with jQuery before you may have noticed that the jQuery function is overloaded. It means that when calling the function with different kinds of argument it behaves in different ways.

For example, when called with a string selector it just returns the elements matching that selection.

//selects the element matching that id
//selects the elements matching that class

When invoked with a string containing a tag the jQuery function creates a new element.


When called with a function it runs the function when the DOM is loaded.

$(function() {
//run on load

No support for function overloading


Quick start with try, catch, throw, and finally

There are cases when functions fail due to an unexpected problem. A common practice is to throw exceptions in these cases and have a part of the code handling them. This is what JavaScript does.

For example, trying to change a frozen object results in an exception. Here is an example.

"use strict";
const obj = Object.freeze({});
obj.msg = "Hi";
//Uncaught TypeError:
//Cannot add property msg, object is not extensible

Execution Stops

The execution of the current function stops when an error is thrown. The statements after that are not executed.

Consider the next example trying to access a property on null

Learn to use the Array.from, split, reverse and join methods

In this article, we are going to reverse a string first using the for loop and then with the array methods. In both cases, we will take into consideration also characters that do not fit in 16-bits. In the end, you can decide which implementation fits your needs.

Reverse a string using the for loop

When using a loop we start by defining a variable (reversedText) storing the transformed text and initialize it with an empty string.

Then we iterate over the list of characters and at each step concatenate a new character.

function reverse(text){ let reversedText = ''; for(let i = text.length - 1; i>= 0…

The OR operators, the nullish operators, function defaults, and more

decorative: closeup of a surface covered in grommets
decorative: closeup of a surface covered in grommets

Setting default values on variables or parameters is a common scenario we may come across. In this article, you will find six different options to do that.

Let’s begin.

1. The OR (||) Operator

The OR (||) operator can be used for deciding defaults.

When the first operand is truthy OR (||) returns the first operand and the second is not evaluated. When the first operand is falsy, then it returns the second operand.

As you’ve noticed, the OR (||) does not necessarily return true or false, but it returns the first or the second operand, depending on the case. …

The split and map methods, the Array.from utility, and more

Knowing how to convert a number into an array of digits can come in handy when transforming those digits.

Let’s find out how to do that.

The first approach implies converting the number to a string, split the string into an array of number characters then convert that into an array of numbers.

const number = 12345;
const digits = String(number)

//[1, 2, 3, 4, 5]

The String built-in function converts any value into a string.


The split method splits a text into an array of substrings based on a separator. We can use the…

Get-up-to speed with closures and encapsulation in JavaScript

Before answering this question we need to first acknowledge that in JavaScript functions can be defined inside other functions. Below is such an example.

function outer(){
function inner(){}

As you can see the inner function is defined inside the outer function.

Not only that we can defined functions inside other functions but the inner function can access variables and parameters from the outer function. Let’s extend the previous example.

function outer(){
const x = 1;
function inner(){

Even more, the inner function has the ability to access variables and parameters from the outer function even after…

substr, substring, slice, and more

There are three methods for extracting part of a string in JavaScript, maybe too many. One would have been enough


The substr(start, length) method extracts part of a string, beginning at the specified index and returning the specified number of characters.

const quote = "Winter is coming";const part1 = quote.substr(0, 6);
const part2 = quote.substr(10, 6);

Notice that the first character is at index 0.

The start index is required but the length is optional. If omitted it extracts the rest of the string.

const quote = "Winter is coming";const part = quote.substr(6);
// is coming


Quick-start with declaring and initializing variables in JavaScript

There are three ways for declaring variables in JavaScript. We are going to discuss all and look at the differences between them.


The var statement was the initial way for declaring variables but has started to become obsolete.

It is optional to initialize the var variables. An uninitialized variable stores the undefined value.

var name;console.log(name);

When declaring a variable with var inside a function that variable is available everywhere in that function.

Variables declared with var have no block scope. Consider the next example.

var name = 'Jon Snow';
//'Jon Snow';

Variables declared with var

The this parameter, privacy with classes are just a few pitfalls. Factory functions can be a better alternative.

Sun peaking through the trees
Sun peaking through the trees

A few years ago, I wrote an article pointing out the reasons why I didn’t like classes in JavaScript and I preferred using factory functions. Three years later, I can still say that I don’t like classes in JavaScript and I use them only as an exception when there are no alternatives.

The good news is that many of the class-related problems have been solved. This is great because there are still applications using them, so now there are fewer things to worry about.

Privacy With Classes

Class properties are public by default. That means we can access and modify any of the…

Cristian Salcescu

Author of Functional Programming in JavaScript. Enthusiastic about sharing ideas.

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