Some important Javascript method you need to know it.

prince shafayat
3 min readNov 5, 2020

--

Javascript is a popular programming language. Some of time learner confuse to learn javascript. So today we discuss with javascript some important method with example.

  1. trim()

trim() remove strings extra white space on start or end points.

example:

const myMessage = "      I love my country";const myMessage2 = "       I love my family         ";console.log(myMessage.trim())// expected output: " I love my country ";console.log(myMessage2.trim())// expected output: " I love my family ";

2. toLowercase, toUppercase

* All character convert to small letter when use toLowercase.

const myTxt = "My First Baby Name is Abdullah";console.log(myText. toLowercase());// expected output: " my first baby name is abdullah ";

* All character convert to capital letter when use toUppercase.

const myTxt = "My First Baby Name is Abdullah";console.log(myText. toUppercase ());// expected output: " MY FIRST BABY NAME IS ABDULLAH ";

3. parseFloat, parseInt

* The parsFloat check have any number in the string or string starting point. if have it convert string to float number.

const number = "124abdce";console.log(number.parsFloat());// expected output: 124

* The parseInt check have any number in the string or string starting point. if have it convert string to int number.

const number = "124abdce";console.log(number.parseInt ());// expected output: 124

4. ceil, floor, round

* ceil

some of time we need to round our float value to int number. The ceil round the number to the next largest int number.

myNumber = 4.35;console const.log(myNumber.ceil())// expected output: 5

* floor

floor is same as ceil. but floor round the number to the lowest integer.

const myNumber = 4.35;console.log(myNumber. floor ())// expected output: 4

* random

random is also round the name like as ceil & floor. but the different is random return the nearest integer value.

const myNumber = 4.35;const myNumber2 = 4.65;console.log(myNumber. floor ())// expected output: 4console.log(myNumber2. floor ())// expected output: 5

5. random

random is return 0 to 1 floating number. many of time we need to random value or number. Then we can use the random function as we need.

for example we need return maximum value 5. then we can use it like the following example.

function getRandomSpeed (max){return const math.floor(math.random() * max);}console.log(getRandomSpeed(5));// expected output: 0,1,2,3,4console.log(math.random());// expected output: 0 < 1

Another example Now we need set minimum & maximum value . then we can use it like the following example.

function getRandomSpeed (min, max){return const math.floor((math.random() * (max-min)) + min);}console.log(getRandomSpeed(5, 10));// expected output: 5,6,7,8,9

6. concat()

if you need to marge two or more array then you can use concat().

concat() marge the array but not change any of the value.

const fruitsList = ["apple", "banana", "pineapple"];const vegetableList = ["tomato","black beans"];const foodList = fruitsList.concat(vegetableList);console.log(foodList);// expected output: ["apple", "banana", "pineapple", " tomato ", " black beans "];

7. map()

map() loop data like as for, while, foreach. map() need a call back function for return new value.

const carList = ["toyota", "BMW" , "tata", "kia", "mini"];const showCarList = carList.map(car=> car );console.log(showCarList);// expected output:  Array ["toyota", "BMW", "tata", "kia", "mini"]

8. join()

join() convert the array to the string and also join() separate the string as declare separator. default separator is ( , ) coma.

const carList = ["toyota", "BMW" , "tata", "kia", "mini"];const showCarList = carList.map(car=> car ).join( " ");const showCarList2 = carList.map(car=> car ).join(", ");const showCarList3 = carList.map(car=> car ).join( " - ");console.log(showCarList);// expected output:  "toyota BMW tata kia mini"console.log(showCarList2);// expected output:  "toyota, BMW, tata, kia, mini"console.log(showCarList3);// expected output:   "toyota - BMW - tata - kia - mini"

9. push()

If you we need to add one or more new element to the array. Then we can use push(). push() method add new element to the end of the array.

const carList = ["toyota", "BMW" , "tata"]carList.push("kia", "mini");console.log(carList);// expected output:  Array ["toyota", "BMW", "tata", "kia", "mini"]

10. pop()

pop() is alternative the push() method. pop() remove & return last element of the array.

const carList = ["toyota", "BMW" , "tata"]const lastAddedCar = carList.pop();console.log(lastAddedCar);// expected output:  " tata "console.log(carList);// expected output:  Array ["toyota", "BMW"]

Hope this article helped you !!!

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

prince shafayat
prince shafayat

No responses yet

Write a response