Built-in JavaScript functions useful for Developers

JavaScript has several “top-level” built-in functions. JavaScript also has four built-in objects: Array, Date, Math, and String. Each object has special-purpose properties and methods associated with it. JavaScript also has constructors for Boolean and Number types.

In this article are the brief definitions and implementation of some of the active JavaScript utilities available for arrays and strings.

Array Methods

.find()

Returns the first element that satisfies the logic provided in the callback.

const array1 = [5, 12, 8, 130, 44];

const found = array1.find(element => element > 10);

console.log(found);
// expected output: 12

.findIndex()

Returns the index of the first element that satisfies the logic provided in the callback.

let arr = [3,6,9,10,15]
arr.findIndex(ele => ele % 5 === 0) // 3
arr.findIndex(ele => ele % 3 === 0) // 0
arr.findIndex(ele => ele % 15 === 0) // 4

.slice()

Can be used on an array or string. It returns a new array of elements or string of characters from the two indexes that are passed as parameters. If just one parameter is passed in, all elements or characters from the initial index are returned.

let string = "Hello world"
string.slice(2,5) // "llo "
string.slice(2) // "llo world"

let arr = [1,2,3,4,5,6,7,8,9]
arr.slice(4,8) // [5,6,7,8,9]
arr.slice(2) // [3,4,5,6,7,8,9]

String Methods

.charAt()

Returns the character of a given index in a string.

let string = 'Hello world'
string.charAt(1) // e
string.charAt(4) // o
string.charAt(10) // d

.charCodeAt()

Returns the ASCII character code of a character at a given index in a string.

let string = 'Hello world'
string.charCodeAt(1) // 101
string.charCodeAt(4) // 111
string.charCodeAt(10) // 100

.match()

Returns the capturing group of the first word/group of characters in a string that matches the parameter passed in. When used with the g flag in a regular expression, .match() will return an array if more than one match is found.

var str = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
var regexp = /[A-E]/gi;
var matches_array = str.match(regexp);

console.log(matches_array);
// ['A', 'B', 'C', 'D', 'E', 'a', 'b', 'c', 'd', 'e']

.substr()

Works similar to .slice() but only works on strings and the second parameter indicates the number of characters that will be return. If the second parameter is not provided, all characters from the initial index point will be returned.

let string = 'Hello world'
string.substr(2,2) // 'll'
string.substr(2) // 'llo world'

This article is the end, hope it will be of help to you. Thanks for reading .

#software-development #javascript #software-engineering #technology #web-development

Built-in JavaScript functions useful for Developers
27.85 GEEK