10 popluar Javascript functions which every javascript developer should know

Today, JavaScript is at the core of virtually all modern web applications. The past several years in particular have witnessed the proliferation of a wide array of powerful JavaScript-based libraries and frameworks for single page application (SPA) development, graphics and animation, and even server-side JavaScript platforms. JavaScript has truly become ubiquitous in the world of web app development and is therefore an increasingly important skill to master.

Here, we’ve pulled together a dozen or so items from this year for you to check out, or revisit. So, here’s a reminder of what proved popular in the world of JavaScript in 2018.

In this post i will share 10 JavaScript array methods you should know.

1. forEach()

This method can help you to loop over array’s items.

  const arr = [1, 2, 3, 4, 5, 6];

  arr.forEach(item => {
    console.log(item); // output: 1 2 3 4 5 6
  });

 

2. includes()

This method check if array includes the item passed in the method.

  const arr = [1, 2, 3, 4, 5, 6];

  arr.includes(2); // output: true
  arr.includes(7); // output: false

 

3. String.prototype.toLowerCase()

string.toLowerCase()

string.toLowerCase() description

The toLowerCase() function does exactly what you think it would. It simply returns a new string that has been converted to all lower case from the old string. The original string is not changed.

string.toLowerCase() function examples

This is a really simple example.

This example has a little bit of a better use case. Let’s build a sluggify function that takes a string, and turns it into it’s slug form.


4. String.prototype.trim()

string.trim()

string.trim() description

The trim() function removes any whitespace characters from both the beginning and ending of a particular string.

string.trim() function examples

trim() works great for data sanitization and other means of cleaning up data before operating on it.


5. String.prototype.charAt()

string.charAt(index)

string.charAt() description

The charAt() function stands for character at. You can find the exact location of a given character in a string when you use this function. It almost reads like, “Tell me the character at position x”. In order for the charAt() function to do it’s job, you must provide an index parameter between 0 and one less than the length of the string you would like to perform the charAt() function on. If the index provided as a parameter is not between 0 and string.length − 1, this function returns an empty string.

This kinds of works like indexOf() in reverse. With charAt(), you provide a numerical value, and charAt() tells you the character that lives at that location. With indexOf(), you provide the character, and indexOf() tells you the index location it lives at.

string.charAt() function examples


6. String.prototype.charCodeAt()

string.charCodeAt(index)

string.charCodeAt() description

The charCodeAt() function works almost like charAt() except instead of returned the character at a specific index position, it returns the encoding of the character at a given position in the string.

string.charCodeAt() function examples

Here we will use the alphabet as a string, and learn the Unicode number associated with every letter by making use of charCodeAt().

The output above is the Unicode number of each letter of the alphabet in lowercase.


7. String.prototype.toUpperCase()

string.toUpperCase()

string.toUpperCase() description

Just like you think it might, the toUpperCase() function turns a string into an all uppercase version of itself. Do note that the original string is left untouched, while the returned string is the all uppercase version.

string.toUpperCase() function examples


8. Boolean Methods

Here is a list of each method and its description.

Sr.No. Method & Description
1 toSource()Returns a string containing the source of the Boolean object; you can use this string to create an equivalent object.
2 toString()Returns a string of either “true” or “false” depending upon the value of the object.
3 valueOf()Returns the primitive value of the Boolean object.

9. String Methods

Here is a list of each method and its description.

Sr.No. Method & Description
1 charAt()Returns the character at the specified index.
2 charCodeAt()Returns a number indicating the Unicode value of the character at the given index.
3 concat()Combines the text of two strings and returns a new string.
4 indexOf()Returns the index within the calling String object of the first occurrence of the specified value, or -1 if not found.
5 lastIndexOf()Returns the index within the calling String object of the last occurrence of the specified value, or -1 if not found.
6 localeCompare()Returns a number indicating whether a reference string comes before or after or is the same as the given string in sort order.
7 length()Returns the length of the string.
8 match()Used to match a regular expression against a string.
9 replace()Used to find a match between a regular expression and a string, and to replace the matched substring with a new substring.
10 search()Executes the search for a match between a regular expression and a specified string.
11 slice()Extracts a section of a string and returns a new string.
12 split()Splits a String object into an array of strings by separating the string into substrings.
13 substr()Returns the characters in a string beginning at the specified location through the specified number of characters.
14 substring()Returns the characters in a string between two indexes into the string.
15 toLocaleLowerCase()The characters within a string are converted to lower case while respecting the current locale.
16 toLocaleUpperCase()The characters within a string are converted to upper case while respecting the current locale.
17 toLowerCase()Returns the calling string value converted to lower case.
18 toString()Returns a string representing the specified object.
19 toUpperCase()Returns the calling string value converted to uppercase.
20 valueOf()Returns the primitive value of the specified object.

10. String HTML wrappers

Here is a list of each method which returns a copy of the string wrapped inside the appropriate HTML tag.

Sr.No. Method & Description
1 anchor()Creates an HTML anchor that is used as a hypertext target.
2 big()Creates a string to be displayed in a big font as if it were in a <big> tag.
3 blink()Creates a string to blink as if it were in a <blink> tag.
4 bold()Creates a string to be displayed as bold as if it were in a <b> tag.
5 fixed()Causes a string to be displayed in fixed-pitch font as if it were in a <tt> tag
6 fontcolor()Causes a string to be displayed in the specified color as if it were in a <font color=”color”> tag.
7 fontsize()Causes a string to be displayed in the specified font size as if it were in a <font size=”size”> tag.
8 italics()Causes a string to be italic, as if it were in an <i> tag.
9 link()Creates an HTML hypertext link that requests another URL.
10 small()Causes a string to be displayed in a small font, as if it were in a <small> tag.
11 strike()Causes a string to be displayed as struck-out text, as if it were in a <strike> tag.
12 sub()Causes a string to be displayed as a subscript, as if it were in a <sub> tag
13 sup()Causes a string to be displayed as a superscript, as if it were in a <sup> tag

11. Array Methods

Here is a list of each method and its description.

Sr.No. Method & Description
1 concat()Returns a new array comprised of this array joined with other array(s) and/or value(s).
2 every()Returns true if every element in this array satisfies the provided testing function.
3 filter()Creates a new array with all of the elements of this array for which the provided filtering function returns true.
4 forEach()Calls a function for each element in the array.
5 indexOf()Returns the first (least) index of an element within the array equal to the specified value, or -1 if none is found.
6 join()Joins all elements of an array into a string.
7 lastIndexOf()Returns the last (greatest) index of an element within the array equal to the specified value, or -1 if none is found.
8 map()Creates a new array with the results of calling a provided function on every element in this array.
9 pop()Removes the last element from an array and returns that element.
10 push()Adds one or more elements to the end of an array and returns the new length of the array.
11 reduce()Apply a function simultaneously against two values of the array (from left-to-right) as to reduce it to a single value.
12 reduceRight()Apply a function simultaneously against two values of the array (from right-to-left) as to reduce it to a single value.
13 reverse()Reverses the order of the elements of an array — the first becomes the last, and the last becomes the first

12. Javascript Object.freeze()

The Object.freeze() method freezes an object. A frozen object can no longer be changed; freezing an object prevents new properties from being added to it, existing properties from being removed, prevents changing the enumerability, configurability, or writability of existing properties, and prevents the values of existing properties from being changed. In addition, freezing an object also prevents its prototype from being changed. freeze() returns the same object that was passed in.

 Syntax  Object.entries(obj)

 Return valueThe object that was passed to the function.

console.clear();

const object1 = {
  property1: 42
};

const object2 = Object.freeze(object1);

object2.property1 = 33;
// Throws an error in strict mode

console.log(object2.property1);
// expected output: 42

13. Javascript Object.keys()

The Object.keys() method returns an array of a given object’s own property names, in the same order as we get with a normal loop.

 Syntax  Object.keys(obj)

 Return value An array of strings that represent all the enumerable properties of the given object.

Where to use:

  • Object.keys() is used for returning enumerable properties of a simple array.
  • Object.keys() is used for returning enumerable properties of an array-like object.
  • Object.keys() is used for returning enumerable properties of an array like object with random key ordering.
console.clear();

var check = ['car', 'plane', 'ship'];
console.log(Object.keys(check));
//expected output: ["0", "1", "2"]

var object = { 1: 'x', 2: 'y', 3: 'z' };
console.log(Object.keys(object));
//expected output: ["1", "2", "3"]

14. Javascript Object.entries()

The Object.entries() method returns an array of a given object’s own enumerable string-keyed property [key, value] pairs.

The order of the array returned by Object.entries() does not depend on how an object is defined. If there is a need for certain ordering then the array should be sorted first like Object.entries(obj).sort((a, b) => b[0].localeCompare(a[0]));.

 Syntax  Object.entries(obj)

 Return value An array of the given object’s own enumerable string-keyed property [key, value] pairs.

const object1 = { foo: 'bar', baz: 42 };
console.log(Object.entries(object1)[1]);
// expected output: Array ["baz", 42]

const object2 = { 0: 'a', 1: 'b', 2: 'c' };
console.log(Object.entries(object2)[2]);
// expected output: Array ["2", "c"]

const result = Object.entries(object2).sort((a, b) => a - b);
console.log(Object.entries(result)[1]);
// expected output: Array ["1", Array ["1", "b"]]
15. Math Methods

Here is a list of each method and its description.

Sr.No. Method & Description
1 abs()Returns the absolute value of a number.
2 acos()Returns the arccosine (in radians) of a number.
3 asin()Returns the arcsine (in radians) of a number.
4 atan()Returns the arctangent (in radians) of a number.
5 atan2()Returns the arctangent of the quotient of its arguments.
6 ceil()Returns the smallest integer greater than or equal to a number.
7 cos()Returns the cosine of a number.
8 exp()Returns EN, where N is the argument, and E is Euler’s constant, the base of the natural logarithm.
9 floor()Returns the largest integer less than or equal to a number.
10 log()Returns the natural logarithm (base E) of a number.
11 max()Returns the largest of zero or more numbers.
12 min()Returns the smallest of zero or more numbers.
13 pow()Returns base to the exponent power, that is, base exponent.
14 random()Returns a pseudo-random number between 0 and 1.
15 round()Returns the value of a number rounded to the nearest integer.
16 sin()Returns the sine of a number.
17 sqrt()Returns the square root of a number.
18 tan()Returns the tangent of a number.
19 toSource()Returns the string “Math”.

Source: www.tutorialspoint.com/ | https://vegibit.com/

Comments