Types: The Building Blocks of JavaScript

Shakil Ahmed Atik
3 min readMay 6, 2021
Cover Photo

In accordance with reality types are the main building blocks of any programming language. So, one must master types in order to learn a programming language. Today I am writing about three most important types (Strings, Numbers & Arrays) in JavaScript and some of their most used methods. I will try to explain each method with example. Lets start with Strings first…

Strings

Anything written inside quotes is considered as string in JS. Quotes can be single or double quotes. Quotes can be used inside a string, as long as they don’t match the quotes surrounding the string.

JavaScript provides a wide range of string methods to help us working with strings. Among all these methods, there are some very common and useful methods that I am going to explain here with examples. Every JS developer should know them.

1. String.prototype.charAt()

This charAt() method returns the character at the specified offset into the string.

2. String.prototype.includes()

This method performs a case-sensitive search to determine whether one string may be found within another string, returning true or false as appropriate.

3. String.prototype.indexOf()

This method returns the index of the first occurrence of a specified text in a string.

Numbers

JavaScript has only one type of number. Numbers in JS can represent fractional values, but there are some limits to what it can store.

1. Number.parseInt()

This parses a string and returns a whole number.

2. Number.isNaN()

The Number.isNaN() method determines whether a value is NaN (Not-A-Number).

Arrays

Arrays are one of the most useful type of JS. In array we can store multiple values in a single variable.

1. Array.prototype.map()

This method allows us to iterate over an array and modify its elements using a callback function. Map method creates a new array populated with the results of calling a provided function on every element in the calling array.

2. Array.prototype.pop()

The pop() method removes the last element of an array, and returns that element. This method changes the length of an array.

3. Array.prototype.reduce()

JavaScript’s reduce method is one of the cornerstones of functional programming. The reduce() method on JavaScript arrays executes a “reducer” function on every element of the array in order, passing the return value from the previous reducer call to the next reducer call. The reduce() function is a common cause of confusion, but it can also make your code much more readable when combined with other functional programming abstractions.

4. Array.prototype.find()

The JavaScript Array.find method is a convenient way to find and return the first occurence of an element in an array, under a defined testing function. When you want a single needle from the haystack, reach for find()!

5. Array.prototype.forEach()

The forEach method is also used to loop through arrays, but it uses a function differently than the classic “for loop”.

The forEach method passes a callback function for each element of an array together with the following parameters:

Thank You~!

--

--