JavaScript string

  1. toUpperCase():

This method will let you convert uppercase letters to a capital letter. Like, “ Have a good day.” toUpperCase() the method will convert it to: “HAVE A GOOD DAY.” It converts all lower case letters in array to upper case. This function has a cousin called toLowercase(). This method can change uppercase letters to lowercase. Like, “have a good day.”

var wish = “I’ll buy you a car.”;console.log(wish.toUpperCase()) // I'LL BUY YOU A CAR.
console.log(wish.toLowercase()) // i'll buy you a car.

2. concat()

Theconcat() method connects two string arguments and provides another string. This function can be used to add two arrays.

 str1 = ['a', 'b', 'c']; str2 = ['d', 'e', 'f'];console.log(str1.concat(str2));
// ['a', 'b', 'c', 'd', 'e', 'f']
console.log(str2.concat(str1));
// ['f', 'e', 'd', 'c', 'b', 'a']

3. lastIndexOf():

The lastIndexOf() method finds the index of the last occurrence This method is also case-sensitive.

 foo = 'I hate dogs. so I stay away from dogs' serchItem = 'dogs'console.log(foo.lastIndexOf(serchItem))
// The index of searchItem is 33

JavaScript number

4. parseInt():

The parseInt() method parses a string argument and converts it into an integer value. parseInt() returns the first number in the string. If the first is character cannot be converted then parseInt() will NaN.

var num1 = 20.2;
console.log(parseInt(num1));
// 20

5. parseFloat():

The parseFloat() function converts a string value to a floating-point number. It only returns the first number in the string. If the first is character cannot be converted then parseFloat() will NaN.

 f = parseFloat("60 years");
g = parseFloat("He is 60 years old");
console.log(f)
// 60
console.log(g)
// NaN

6. isNaN():

The isNaN() method determines whether the passed value is a Number or not.

function feetToInch(x) {
if (isNaN(x)) {
return 'Not a Number!';
}
return x * 12;
}
console.log(feetToInch(2), 'inch')
// 24 inch

JavaScript Array:

7. foEach():

The forEach() method calls a function for each element in the array. It can take three parameters, currentValue, index, array. The currentValue parameter will return the current value of the element in the loop. The index parameter tells the index number of the element in the loop. And the array parameter returns the whole array.

var array1 = [30, 20, 10];array1.forEach(element => console.log(element));
// 30
// 20
// 10
array1.forEach((currentValue, index, array) => console.log(currentValue, index, array));
// 30 0 [ 30, 20, 10 ]
// 20 1 [ 30, 20, 10 ]
// 10 2 [ 30, 20, 10 ]

8. splice(): The splice() method removes items from an array and returns an array of rest items without removed items and if necessary inserts new elements in their place. Splice changes the original array. It takes two index values; the first is for a starting index and the second is for the number of elements to remove.

 nums = [1, 2, 32, 435, 4, 6, 456, 46];console.log(nums.splice(2, 5));
// [ 32, 435, 4, 6, 456 ]
console.log(nums); // Now splice changed the array.// changed array output: [1, 2, 46]

9. slice(): The slice() method returns a shallow copy of a segment of an array into a new array where the starting number represents the index of the item in that array and the ending number represents the previous element of the selected index in that array. The slice() method doesn’t change the original array.

 nums = [1, 2, 32, 435, 4, 6, 456, 46];console.log(nums.slice(2, 6));
// [ 32, 435, 4, 6 ]

10. reduce():

The reduce() method executes for a provided function for each element of the array and reduces that array to a single value. Here’s a simple explanation with an example:

 numbers = [1, 2, 32, 435, 4, 6, 456, 46]; total = nums .reduce((, ) ( + ), 0)console.log(total);
// 982

Here the sum or reduced value of all the elements of the numbers array is 982.
The default value of the sum parameter is 0. The num parameter is adding one element to the sum parameter from left to right of the numbers array. Any value can be taken as the default value based on the elements of the array.

console.log(sum, num);// 1 0
// 2 1
// 32 3
// 435 35
// 4 470
// 6 474
// 456 480
// 46 936

The values of sum and num parameters are shown in the console above.

Full stack web developer || Experienced with JavaScript , React js, ES6, Node js, Express, Mongo DB,

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store