Loved by some and hated by others. The truth is that we can’t live without it.

JavaScript has a significant part in our lives yet, so many developers choose to fear or hate it. This hate is probably due to the actual amount of mediocre implementations, outdated guides, and books, inexperienced people, or constant comparison with other languages (despite sharing the prefix, JavaScript has nothing to do with Java).

LiveScript, as it was to be called originally, was created in 1995, being first released in the subsequent year.

After submission to the European standards organization Ecma international, ECMAScript was born, defining a bunch of standards and features to be used in JavaScript. Despite these standards being set, it wasn’t until 2009 and recently 2015 that they helped shape the language in what it is today.

This article is the first of a series where the main focus is to help all the readers get up-to-date with today JavaScript.

What is JavaScript?

JavaScript is a language with first-class functions. First-class functions mean that the functions can be treated like any other variable. That’s the same thing as saying that functions can be returned from functions, assigned as values to variables, and receive them as parameters on your functions.

One thing you may or may not know is that JavaScript supports object-oriented programming and many other programming paradigms.

Depending on the host environment, JavaScript can be interpreted or just-in-time compiled.

Now let us discuss types and values.

Values and Types

First of all, it’s essential to keep in mind that in JavaScript, the created variables do not have a type, its values do.

The primitive types in JavaScript are the following:

Number

Boolean

String

Object

Null

Undefined

Symbol

The first four types should be familiar to everyone.

Number is used to define a numerical value on JavaScript. Contrary to other languages, JavaScript doesn’t have the notion of integer or decimal values, and this should be taken into account when doing some mathematical operations in JavaScript.

String is used to define characters or a sequence of characters. In JavaScript, they can be declared inside of single quotes or double-quotes.

Boolean is used to define boolean logic. This logic can be represented through the reserved keywords, true or false.

Object is used to define an object wrapper. Every non-primitive type in JavaScript is said to be a subtype of Object. Some examples are Function, Array, or Date Objects.

Undefined and Null represent the absence of value in JavaScript. Undefined is used to represent the unintentional absence of a value, while Null represents the intentional absence of value.

Last but not least, the most recently introduced type is Symbol. Symbols are used to define a unique value. This means that even if you add the same number or string to a Symbol, they’ll always be different from each other.

Shall we see an example now? Using the operator typeof, we can obtain a string with the type of the given value.

types.js

The example should be pretty straight forward if you read the descriptions above. One thing that may cause confusion is why is the typeof null returning an object?

This is a well known historical bug in JavaScript, and because of some legacy reasons, it can’t be fixed.

Variables

Variables in JavaScript can be created using one of three keywords: var, let, or const.

var was the traditional way to declare a variable in JavaScript. All the variables declared with the var keyword have a function scope and can be accessed accordingly.

let and const were introduced in ECMAScript 2015 (ES6) and allow us to declare block-scoped variables. The only difference between let and const is that const is meant to be used on variables whose values aren’t intended to change.

variables.js

Loops

Loops allow executing specific tasks a given number of times. This may be, for example, moving between the elements of an array.

JavaScript supports the traditional for and while loops. More recently (ES6), for…of was introduced and allowed looping between elements of an array or map to be more readable and straightforward than before.

loops.js

Functions

As mentioned above, functions are sub-types of objects, and, thanks to this, they can have properties, methods and also be assigned to variables.

Functions are created through the function keyword or, thanks to ES6, through a fat arrow ( => ).

Bellow, we can see various types of function declarations in JavaScript.

functions.js

Let’s now analyze each example.

A procedure

In the first one, we can see a procedure (Procedures are functions that don’t return anything) that receives a parameter called message, and by calling the global object console and it’s log method, the parameter is displayed in the user console. It’s important to mention that even though there isn’t a return statement, this function returns undefined by omission.

Subtraction function and assignment to a block-scoped variable

Continuing to the second function, here we receive two parameters and return the subtraction of them. It’s also in this example that we see that a function call can be saved inside a variable. After that assignment, we can call subtracted all over our block and get the output of the subtraction between the given numbers.

Arrow functions and default parameters

The third and fourth functions show us an example of an arrow function. These arrow functions work exactly like the procedure in the first example.

Paying particular attention to the parameters of the fourth example, we can see that there is a string assigned to the message variable. This is called default parameters, and what they do is, if no parameter is passed when invoking this function, then by default, that parameter has the default string assigned.

Callback function

The last example shows us a callback function. Callback functions are functions that are passed as parameters to other functions and invoked by these functions to indicate the end of an action.

Conclusion

By now, you had a tiny taste of some of the cool things that JavaScript has to offer.

Types, variables, loops, and functions are the basics you need to have to do something with JavaScript. I suggest that using some of the examples provided above, you write a Hello World function and try to apply some of the knowledge gained.

In the next part of this series, the fun begins. We’ll first focus on coercion so that then we can learn how equality checks work in JavaScript and the difference between double and triple equality check. Then we’ll continue to learn about scope and closures.

I hope you enjoyed and stay tuned for the next guides.

To all of you, have a great week!