DevPremier
Ashwani Garg must to know Javascript functions, which can boost your developing speed. JavaScript is a cross-platform, object-oriented scripting language used to make webpages interactive Posted at:

Must to know Javascript functions

Must to know Javascript functions

In this article, I'll explain to you the best and must to know Javascript functions, which can boost your developing speed.

JavaScript (JS) is a lightweight interpreted or JIT-compiled programming language with first-class functions. While it is most well-known as the scripting language for Web pages, many non-browser environments also use it, such as Node.js, Apache CouchDB and Adobe Acrobat. JavaScript is a prototype-based, multi-paradigm, dynamic language, supporting object-oriented, imperative, and declarative (e.g. functional programming) styles.

These JavaScript methods will improve your skills in just a few minutes

JavaScript is a cross-platform, object-oriented scripting language used to make webpages interactive (e.g. having complex animations, clickable buttons, popup menus, etc.).  There are also more advanced server-side versions of JavaScript such as Node.Js which allow you to add more functionality to a website than simply downloading files (such as real-time collaboration between multiple computers). Inside a host environment (for example, a web browser), JavaScript can be connected to the objects of its environment to provide programmatic control over them.

JavaScript contains a standard library of objects, such as Array, Date, and Math, and a core set of language elements such as operators, control structures, and statements. 

Arrays are a powerful and comprehensive tool of Javascript. They are very intuitive to use and you can do almost everything with it. However, there are important differences between arrays in Javascript and other mainstream languages. Knowing them will help you unleash their true potential.

In this article, we will go through the important aspects of arrays that are imperative to every JS developer and we will see the variety of usages they have.

ARRAYS ARE SPECIAL [Objects]

In Javascript, there are only 6 data types defined – the primitives (boolean, number, string, null, undefined) and object (the only reference type). Arrays do not belong to this list because they are objects as well. This is a common confusion among developers who assume that arrays are a special data type in Javascript.

The items in the array are nothing more than the properties of those objects. You can use any name as property for an object, including numbers or even empty string. However, if the property name is not a valid identifier (doesn’t start with a letter), you can only access it through obj[property_name] and not obj.property_name.

So nothing stop us from taking an object and declaring properties like 0, 1, 2 and so on and accessing through obj[0], obj[1] and so on. You don’t need a special data type for doing that in Javascript.

Javascript Array.length

// returns number of elements in the array
var array = [1,2,'Hello','Ashwani','Garg'];
console.log(array.length);

// expected output: 5

Javascript Array.pop()

array.pop() - Removes the last element from an array and returns that element.

var plants = ['broccoli', 'cauliflower', 'cabbage', 'kale', 'tomato'];

console.log(plants.pop());
// expected output: "tomato"

console.log(plants);
// expected output: Array ["broccoli", "cauliflower", "cabbage", "kale"]

plants.pop();

console.log(plants);
// expected output: Array ["broccoli", "cauliflower", "cabbage"]

Javascript Array.push()

array.push(element) - Adds one or more elements to the end of an array and returns the new length of the array. 

var animals = ['pigs', 'goats', 'sheep'];

console.log(animals.push('cows'));
// expected output: 4

console.log(animals);
// expected output: Array ["pigs", "goats", "sheep", "cows"]

animals.push('chickens');

console.log(animals);
// expected output: Array ["pigs", "goats", "sheep", "cows", "chickens"]

Javascript Array.shift()

array.shift() - Removes the first element from an array and returns that element.

var array1 = [1, 2, 3];
var firstElement = array1.shift();

console.log(array1);
// expected output: Array [2, 3]

console.log(firstElement);
// expected output: 1

Javascript Array.sort()

array.sort() - The sort() method sorts the elements of an array in place and returns the array.

var months = ['March', 'Jan', 'Feb', 'Dec'];
months.sort();
console.log(months);
// expected output: Array ["Dec", "Feb", "Jan", "March"]

var array1 = [1, 30, 4, 21, 100000];
array1.sort();
console.log(array1);
// expected output: Array [1, 100000, 21, 30, 4]

Javascript Array.unshift()

array.unshift(element/s) -The unshift() method adds one or more elements to the beginning of an array and returns the new length of the array.

var array1 = [1, 2, 3];

console.log(array1.unshift(4, 5));
// expected output: 5

console.log(array1);
// expected output: Array [4, 5, 1, 2, 3]

These are the Must to know Javascript functions, I call them Magic functions, cause they work on data like a magician.

map() – returns a new list with the result of each item in an array

indexOf() – returns the first index of the given element in an array

every() – returns true if the function returns true on every item

filter() – returns an array of all items for which the function returns true

forEach() – no return value (just run the function on every element in the list)

some() – returns true if the function returns true for at least one of the items

Javascript array.map()

Array.map() - map() method creates a new array with the result of calling a provided function on every element in the array.

map() calls a provided callback function once for each element in the array, in order and constructs a new array from the results.

var arr1 = [1,2,4,6,10];
const map1 = arr1.map(x => x*2);
console.log(map1);

// expected output: [2,4,8,16,20]

 

Javascript array.indexOf()

array.indexOf() method returns the first index, at which a given element can be found in the array or -1 if it is not found.

var array2 = ['Ashwani', 'Amar', 'John', 'Danial', 'Richard'];
console.log(array2.indexOf('Ashwani'));
// expected output: 0

console.log(array2.indexOf('John'))l
// expected output: 2 

console.log(array2.indexOf('India'))l
// expected output: -1

Javascript array.filter()

array.filter() method creates a new array with all elements that pass the test implemented by the provided function.

var words = ['spray','limit','elite','true','America','India','Entertainment'];
const result = words.filter(word => word.length > 6);
console.log(result);

// expected output :-  ['America','Entertainment']

Javascript forEach()

The forEach() method executes a provided function once for each array element.

var array1 = ['a', 'b', 'c'];

array1.forEach(function(element) {
  console.log(element);
});

// expected output: "a"
// expected output: "b"
// expected output: "c"

Javascript some()

The some() method tests whether at least one element in the array passes the test implemented by the provided function.

Note: This method returns false for any condition put on an empty array.

var array = [1, 2, 3, 4, 5];

var even = function(element) {
  // checks whether an element is even
  return element % 2 === 0;
};

console.log(array.some(even));
// expected output: true