JavaScript Functions That You Will Encounter Frequently

Wreet Sarker
3 min readMay 5, 2021

--

JavaScript is a multi-paradigm, scripting programming language. If you are thinking of becoming a web developer or building web applications, Then JavaScript is your go to language. It’s a pretty old language, aging almost 26 in 2021. Hence there a bulk of features in this language upon which it is revolved. To get a good grasp of any language, at first your need yourself to be acquainted with the building blocks of the language. Today we will talk about some core types of JavaScript and their built in functions.

String

Strings are used to represent chain of characters. It is one one the most important types of any programming language. Now let’s have a look at some of the core methods that are built into String data type in JS.

  1. toLowerCase(): This method allows you to convert all the characters in a string sequence into lower case.
const myName = "WREET";
console.log(myName.toLowerCase())
//expected output : "wreet"

2. toUpperCase(): This method converts all the characters of a string to upper case.

const myNewName = "wreet";
console.log(myNewName.toUpperCase())
//expected output : "WREET"

3. trim(): Often, while getting string data from various sources, such as from api calls, might contain white spaces. This might lead to misfunction of your program. To remove white space from both ends, you can use the trim() function.

const unTrimmed = '    How are you doing?    ';
console.log(unTrimmed.trim());
// expected output: 'How are you doing?'

4. split(): split() is one of the most important functions of String object. This function separates the string by a given delimiter and returns an array of substring.

const str = 'Hello everyone, Have a nice day!';
const strArr = str.split(' ');
console.log(strArr);
// expected output: '['Hello', 'everyone,', 'Have', 'a', 'day!']'

5. concat(): As the name suggests, this method is used to concatenate (connect) two or more strings.

const str1 = 'Hello ';
const str2 = 'World!';
console.log(str1.concat(str2));
//expected output 'Hello World!';

6. slice(): Slice method takes a part of a string and returns it without modifying the original string. It takes one integer as a parameter and another optional integer parameter. If one parameter is passed, it returns a string starting from the given parameters index to the end of the string. If two parameters are passed, it returns a string starting from the first parameters index upto but not including the last second parameters index.

const strWhole = 'My name is wreet';
console.log(strWhole.slice(3));
//expected output: 'name is wreet'

7. startsWith(): This method returns a boolean (true or false) whether a string starts with the given substring or not.

const randomStr = 'My name is wreet';
console.log(randomStr.startsWith('My'));
//expected output: 'true'

8. endsWith(): This method returns a boolean (true or false) whether a string ends with the given substring or not.

const randomStr2 = 'My name is wreet';
console.log(randomStr2.endsWith('et'));
//expected output: 'true'

Math

  1. Math.abs(): Returns an absolute value of a number.
console.log(Math.abs(-2));
//expected output: '2'

2. Math.trunc(): Truncates a number after decimal. Returns an integer.

console.log(Math.trunc(10.6));
expected output: '10'

--

--

Wreet Sarker
Wreet Sarker

Written by Wreet Sarker

0 Followers

Materials and Metallurgical Engineering graduate. Machine learning and Data Science enthusiast.

No responses yet