Rahul Mohonto
4 min readMay 5, 2021

--

10 things in JavaScript

1.charAt()

charAt is a method to access string at a specific index position. it reveals the character at that position of the string.it is written as chaAt(index of that string). ex. var one = ‘we are one’; one.charAt(5); output is e.

one.charAt(3); output is a.

if no index is provided output will be 0. If the length exceeds then it will return empty.

And the index also counts the whitespace. So basically it’s a method to display characters at a different position in a string.

2.indexOf()

indexOf() returns the first position of value searched in a string located in multi-position of that index.

Ex. let two = ‘not bad afterall;

Two.indexOf(a) ,ouptput is 5. Look at here, it returns only the first position number of where it stands.

Syntax is indexOf(search value);

The count for index position starts from 0 and end till the (length-1) position of the string. And if the index position is not provided correctly or position that exceeds the length of the string it will return -1.

indexOf() method is case sensitive. Ex . let three = ‘Brie Larson Died Of Corona’;

console.log(three.indexOf(‘larson’)); will return -1, cause the capital letter in string.

3. .slice()

slice is a method to subtract a section from the original string and return the value as a new string.

Syntax : .slice(indexOf)

.slice(begin index, end index );

The end index must be greater than the start/begin index.

Ex. var four = ‘this version is not available for running in space’;

Console.log(four.slice(4), four); output is version is not available for running in space, this version is not available for running in space.

The intersect start and end position can also be set.

Four.slice(6, 18) like this.

And if called like this four.slice(6, -18) ; then it intersects the string from the last characters.

4. toLowerCase()

It sets all the letters in a string to lowercase if there is any uppercase letters.

Example. var five = ‘ Hello Doctor Muyon‘;

console.log(five.toLowerCase()); output ‘hello doctor muyon’

similarly, toUpperCase() sets the letters in a string to all uppercase.

5. filter()

JavaScript filter is a method to make a new array with elements that passed the criteria test of value. It uses a callback function for each element in an array. And if an element fits the required criteria the callback returns true, with which a new array is created. The callback doesn’t visit the new array.

const six = [‘smart’, ‘people’, ‘can’, ‘find’, ‘everything’];

const result = six.filter(word=>(word>4));

console.log(result); output is [‘smart’, ‘people’, ‘everything’];

6. find()

find() method returns the first value or element from an array that passed the test function or required criteria. It doesn’t visit the element added in the array after the find is begun. find() method run the callback function for each index of the array. And when it finds the required value it returns the first position of elements that are present in multi places in that array.

const seven = [‘12’, ‘13’, ‘8’, ‘20’, ‘15’ ];

const result = seven.find(number=>(number>10))

console.log(result); output is 12.

find() does not mutate but callback can. It takes 3 arguments as parameters element, index, array.

7. forEach()

forEach() method executes function on each elements in an array just for once.

The callback will not visit the element added if the invocation of callback starts. It skips the non-assigned values or missing values. forEach() is invoked by three things

1. the value of the element.

2. the index of the element.

3. the array object being traversed.

The assigned value will be the same if the elements being changes after the callback starts and at the time of callback visit the value will be the same, the callback will not visit for an element that has already visited.

8. map()

map() method only applies to the array and it creates a new array for every callback executes for every element in an array.

ex. const eight = [‘6’, ‘8’, ‘140’, ‘45’, ‘69’];

const result = eight.map(num=>num+4)

output is [‘10’, ‘12’, ‘144’, ‘49’, ‘73’];

because of the map each time callback executes, the returned value is added to a new array.

it should not be used when no value will be returned from the callback. The map doesn’t happen on missing or deleted indexes. The callback is invoked with three arguments. Those are the value of the element, the index of the element, and the array object being mapped.

9. shift()

shift() method applies to arrays and on objects that resemble an array. it removed the first element of the array and returned the exsting array. shift() method changes array, which means it affects the index or length of the array being called.

Ex. const nine = [‘45’, ‘4’, ‘6’, ‘70’,’ 81’];

const result = nine.shift();

output is [ ‘4’, ‘6’, ‘70’,’ 81’];

pop() method is similar to shift() method but only applies to the last element of the array. Means it removes the last element of that array being called.

Array.push() method also applies on array and inserts an element to the last index or position in an array.

10. Array.splice()

splice () is a method to insert new elements in a certain position or index in an array.

It can be written as

.splice(position where to insert, 0)

splice(position where to insert, deletecount);

deletecount means remove the element from that array positioned in a specific index from the start of the array. And if the deletecount is set 0 , then no item will be deleted and the length of the array will increase or change.

Ex const ten = [‘hi’, ‘iam’, ‘prince’];

ten.splice(1, 0, ‘there’);

console.log(ten);

output is [ ‘hi’, ‘there’, ‘iam’, ‘prince’ ];

this is how splice is used.

--

--

Rahul Mohonto
0 Followers

I am an engineer. Currently I am studying Material Science & Engineering .And I am also a Front End Web Developer.