Random Javascript

Sayeed Sayem
4 min readNov 2, 2020

String to Number

anyone can easily convert any string into number format (i.e. ‘19’ to 19). There are two built-in functions to do so. First, parseInt(); it takes two-parameter to show the results (string and base). For example, if anyone wants to convert ‘33’(string) to 33(number) first need is to call the parseInt() function. Then inside this function, he has to pass (‘33’, 10). Here ‘33’ is the string which he/she wants to convert and 10 is the base which determines in which format he wants his results. This function will return 33 as a number.

Practical example:

const num=parseInt(‘23’, 10)

console.log(num)//23.

note: If you don’t pass the base by default it’ll take 10.

Second, parseFloat(); it takes an only string as its parameter and returns it in number format. Its base is always 10. if you want to convert a string (‘33.33’) into numbers you just need to call parseFloat() function like this: parseFloat(‘33.33’). in return you’ll get 33.33 as a number.

JavaScript Operators

Javascript has plenty of operators. For doing a sum, there are +, -, *, /, %.
(+) is for add,(-)is for minus, (*) is for multiply, (/) is for divide and lastly,(%) is for the remainder.
(+) is also used for joining two string.

For example,
const firstName = ‘sayeed’
const lastName = ‘sayem’
const fullName = firstName + lastName;
console.log(fullName) // ‘sayeed sayem’;

For comparisons, < ,>,<=,are used in javascript.
== and === are also operators. == is less strict (i.e. 123 == ‘123’// true)
=== is strict (i.e. 123 === ‘123’ //false).

String.charAt()

charAt() method used to get an exact letter of a string in the exact position as it’s name mentioned (charAt() => character at). In this function, it’s not compulsory to pass a parameter. But if you need an exact letter then you must pass a parameter. If you don’t it’ll return the letter of index 0. The index can be 0 to arguments.length -1. If you pass any parameter other than them it’ll return an empty string.

For example:
const text = ‘A brown fox jumped over the wall’;
const letter = text.charAt(5);
const index100 = text.charAt(100);
const noIndex = text.charAt();
console.log(letter, index100, noIndex) // expected result: ‘w’, ‘’, ‘A’;

Array Methods

It’s very easy to remove or add items in an array and it’s fun. push(),pop(), shift(), unshift() are those functions which can be used to do so. To add an item from the last push() are used, and pop() is for removing from the end.
You can also add or remove items from first. To append a new item in the first you can use shift(). And for delete unshift().

indexOf() and lastIndexOf()

indexOf() is a method of javascript. Mainly it is used for getting the index of a string as its name refers. It is case sensitive so that you must be careful while using it. Otherwise, it will return -1.

For example:
const text = ‘Hello World’;
const indexO = text.indexOf(‘o’);
const indexh = text.indexOf(‘h’);
console.log(indexO, indexh) // 4 , -1;

lastIndexOf() is cousin of indexOf(). indexOf() method return results from the first and lastIndexOf() do opposite.

For example:
const text = ‘Hello’;
const lastIndex = text.lastIndexOf(‘l’) // return 3;
const index = text.indexOf(‘l’) // return 2;

replace() and replaceAll()

These two methods are used for replacing letter/word or sub-string to any string. They always return a new string and the main string remains untouched. replace() methods only change the first matched sub-string but replaceAll() methods change the whole matched sub-strings.

For example:
const text = ‘My name is Khan. Khan means tiger’
const newText = text.replace(‘Khan’, ‘Sher’);
const newTxt = text.replaceAll(‘Khan’, ‘Sher’);
console.log(newText, newTxt) //’My name is Sher. Khan means tiger’, ‘My name is Sher. Sher means tiger’

slice() method

slice() method cut a slice from a string as its name refers. It takes up to two parameters one for starting and the other is ends. If you put a negative index for start or ends it’ll start counting its indexes from the right which is by default left.

To clear the idea an example is given below:
const text = ‘sayeed sayem is my name’
const str1 = text.slice(6) // ‘sayem is my name’
const str2 = text.slice(-4) // ‘name’
const str3 = text.slice(2, 5) // ‘yee’
const str4 = text.slice(-4, -1) // ‘nam’

Case Changing

There are 4 methods to convert the case of a string in javascript. Those are toLocaleUpperCase(), toLocaleLowerCase(), toUpperCase(), toLowerCase(). Each of them are almost same with some dissimilarity. ToLocaleUpperCase(), and toLocaleLowerCase() designed for converting cases in local basis.

For example:
const upperCase= ‘SAYEED SAYEM’
const lowerCase = ‘sayeed sayem’
console.log(upperCase.toLowerCase())// ‘sayeed sayem’;
console.log(lowerCase.toUpperCase())// ‘SAYEED SAYEM’;
console.log(upperCase.toLocaleLowerCase(‘EN-US’)// ‘sayeed sayem’;
console.log(lowerCase.toLocaleUpperCase(‘EN-US’)// ‘SAYEED SAYEM’;

String trimming

Trollolololooo…! you can trim a string like your hairs and beards. But you have to trim them with your keyboard. Joke aside, javascript has its own methods to clean a string from white space. For trimming you can use trim(), trimStart(), trimEnd(). Trim() will erase white space from both sides and trimStart() and trimEnd() will erase from left and right respectively.

For Example:
const x = ‘ sayeed sayem ‘
console.log(x.trim()) // ‘sayeed sayem’;
console.log(x.trimStart()) // ‘sayeed sayem ‘;
console.log(x.trimEnd()) // ‘ sayeed sayem’;

Integer

There are plenty of methods in javascript to convert a floating number to an integer. Those methods are Math.round(), Math.ceil(), Math.floor. They work as their names refer. Math.round() convert a number by adding or deducting some floating numbers. Math.round always try to add or deduce small amounts. It will be clear when I’ll cite an example(just wait). Math.ceil() always add numbers to convert and Math.floor() deduce.

For Example:
console.log(Math.Round(10.4)) // 10;
console.log(Math.Round(20.6)) // 21;
console.log(Math.Round(30.5)) // 31;
console.log(Math.ceil(10.01)) // 11;
console.log(Math.ceil(12.99)) // 13;
console.log(Math.floor(10.01)) // 10;
console.log(Math.ceil(12.99)) // 12;

--

--