Fundamentals things you must know about JavaScript methods

Created in 1995, JavaScript name gone a very way since its beginnings.

It was the first scripting language that was natively by web browsers, and to this it gained a competitive advantage over any other language and today it still.

In the begining, JavaScript was not nearly powerful as it is today, and it was mainly used for fancy animations and the marvel known at the time as Dynamic HTML.

JavaScript is also now widely used outside of the browser. The rise of Node.js in the last few years unlocked backend development.

JavaScript is now also the language powering databases and it’s even develop applications, mobile apps, apps, and much more. What started as a tiny language inside the browser is now the most popular language in the world.

string.indexOf:

We want to search our String to see if ‘Amjad’ exists in it, and perhaps do something with it if there’s a match.

For this, we can use indexOf (which exists on String.prototype):

  • const person = ‘Aziz, Habib, Amjad’;
  • const index = person.indexOf(‘Amjad’);
  • console.log(index);

= Amjad

In this blog post I’ll go over the slice() method of JavaScript arrays. slice() method will return a copy of a portion of an array into a new array, depending on the start and the end index we provide to the slice() method, it’s important to note that the end index will not be included.

  • let number = [1,2,3,4,5,6,7]
  • let result = number.slice(2, 5)
  • console.log(result);

= [ 3, 4 ,5]

In this above example, I created a variable called array1 and then I applied slice() to the array1 variable which has values 1,2,3,4,5,6,7. For slice() I first gave the starting index as 2 and the second value as 4.

You can use the toLowerCase() method in JavaScript for converting string to lower case.

You get the returned value in a new string.

  • const string= “JavaScript is awesome”;
  • var newString = string.toLowerCase();
  • console.log(result);

= javascript is awesome

The toUpperCase() method transforms a string to uppercase letters in JavaScript. It doesn’t change the original string and returns a new string equivalent to the string converted to uppercase.

Here is an example:

  • const string = ‘JavaScript World!’;
  • const upperString = str.toUpperCase();
  • console.log(upperString);

= JAVASCRIPT WORLD!

JavaScript’s parseInt function is all about converting a string to an integer. The function takes a string value as an argument and converts it to a numerical value with no decimal places, or alternatively the value NaN. ParseInt has a total of two parameters and thus takes two arguments. The second parameter specifies a radix value (i.e., a value representing the mathematical base that will be used when converting the string value to a number).

  • const x = “1”;
  • console.log(x, typeof x); //1 string
  • x = parseInt(x, 10);
  • console.log(x, typeof x); //

= 1 number

Like the filter method creates a new array with the same length from the original array. But , the filter method can create a new array with the same length or a smaller size array based on a condition.

filter structure:

  • const words = [ ‘spray’, ‘limit’, ‘elite’, ‘exuberant’, ‘destruction’, ‘present’ ];
  • const result = words.filter(word => word.length > 6);
  • console.log(result);

[“exuberant”, “destruction”, “present”]

Converting a number of array elements to a string and separating each element with an odd character will be a challenge you will run into, if it has not happened yet, it will! Fortunately, the Array.prototype.join() provides an elegant solution to this problem.

  • const letter= [‘a’, ‘b’, ‘c’, ‘d’, ‘e’, ‘f’];
  • console.log( letter.join() );

‘a b c d e f’

Array.map is a function that takes a list and a callback as parameters and returns a new list with the callback applied to each item in the original list. Nice and simple. Here’s an example of a map used to double the values of each item in a list.

  • const numbers = [0, 1, 2, 3, 4];
  • const doubled = numbers.map(function(value) {
  • return value * 2;
  • });
  • console.log(doubled);

= [0, 2, 4, 6, 84]

We can use .reduce() on an array and it executes a reducer function (a callback function) on each element and, we can define the logic of this reducer function. This .reduce() also accepts a second argument: initialValue.

  • arr.reduce((accumulator, currentElement, currentIndex, array) => {
  • // your logic goes here…
  • }, initialValue);

That’s it for today. Hope you like this basic informations.