The three JavaScript’s built-in objects, their methods and properties.

Zahirul Islam
3 min readNov 2, 2020

--

This article covers three built-in object String, Number and Array with their methods and properties.

JavaScript Strings

The String object is used to represent and manipulate a sequence of characters. A JavaScript string can be zero or more characters written inside quotes. You can use single or double quotes:

var carName1 = “Volvo XC60”; // Double quotes
var carName2 = ‘Volvo XC60’; // Single quotes

01. String Length

To find the length of a string, use the built-in length property:

var txt = “ABCDEFGHIJKLMNOPQRSTUVWXYZ”;
var sln = txt.length;

String Methods

02. Finding a String in a String

The indexOf() method returns the index of (the position of) the first occurrence of a specified text in a string:

var str = “Please locate where ‘locate’ occurs!”;

var pos = str.indexOf(“locate”);

03. The slice() Method

slice() extracts a part of a string and returns the extracted part in a new string. The method takes 2 parameters: the start position, and the end position (end not included).

var str = “Apple, Banana, Kiwi”;

var res = str.slice(7, 13);

The result of res will be: Banana

04. The substring() Method

substring() is similar to slice(). The difference is that substring() cannot accept negative indexes.

var str = “Apple, Banana, Kiwi”;

var res = str.substring(7, 13);

The result of res will be: Banana

05. Replacing String Content

The replace() method replaces a specified value with another value in a string:

str = “Please visit Microsoft!”;

var n = str.replace(“Microsoft”, “W3Schools”);

JavaScript Numbers

Unlike many other programming languages, JavaScript does not define different types of numbers, like integers, short, long, floating-point etc. JavaScript has only one type of number. Numbers can be written with or without decimals.

06. Converting Variables to Numbers

There are 3 JavaScript methods that can be used to convert variables to numbers:

  • The Number() method
  • The parseInt() method
  • The parseFloat() method

07. The toFixed() Method

toFixed() returns a string, with the number written with a specified number of decimals:

var x = 9.656;
x.toFixed(0); // returns 10
x.toFixed(2); // returns 9.66

JavaScript Array: Summary

JavaScript array is a variable that contains more than one object.

There are two ways of defining an array in JavaScript, but it’s considered a better practice to use array literal method.

JavaScript arrays can store functions, objects, and other arrays.

let fruits = [‘Apple’, ‘Banana’]

08. Push/Pop

The push() method adds a new element to an array (at the end):

var fruits = [“Banana”, “Orange”, “Apple”, “Mango”];
fruits.push(“Kiwi”); // Adds a new element (“Kiwi”) to fruits

The pop() method removes the last element from an array:

var fruits = [“Banana”, “Orange”, “Apple”, “Mango”];
fruits.pop(); // Removes the last element (“Mango”) from fruits

09. Shift/UmShift Elements

Shifting is equivalent to popping, working on the first element instead of the last. The shift() method removes the first array element and "shifts" all other elements to a lower index.

var fruits = [“Banana”, “Orange”, “Apple”, “Mango”];
fruits.shift(); // Removes the first element “Banana” from fruits

The unshift() method adds a new element to an array (at the beginning), and "unshifts" older elements:

var fruits = [“Banana”, “Orange”, “Apple”, “Mango”];
fruits.unshift(“Lemon”); // Adds a new element “Lemon” to fruits

10. Array.map()

The map() method creates a new array by performing a function on each array element.

The map() method does not execute the function for array elements without values.

The map() method does not change the original array.

var numbers1 = [45, 4, 9, 16, 25];
var numbers2 = numbers1.map(myFunction);

function myFunction(value, index, array) {
return value * 2;
}

--

--

Zahirul Islam
Zahirul Islam

No responses yet