8 Very Useful Built-in Functions in JavaScript for Beginners

·

3 min read

JavaScript (commonly known as JS) is undoubtedly one of the most popular (and pretty cool) programming languages with wide usage in both client-side and server-side applications. The MDN documentation defines JS as - "JavaScript (JS) is a lightweight, interpreted, or just-in-time compiled programming language with first-class functions." Here is a list of eight especially useful and important built-in functions in JS for beginners to know -

  1. Array.map() : This JS function or method acts upon each element of an array, performs the specified operation, and returns a new array. Note that the original array is NOT modified in the operation. For example :

     const array = [1, 2, 3];
     const double = array.map(element => element * 2); 
     // Result = [2, 4, 6]
    
  2. Array.filter() : This method filters a given array and returns a new array based on the condition specified for the filter operation to be performed. For example :

     const numArr = [1, 2, 3, 4, 5];
     const evenArr = numArr.filter(item => item % 2 === 0); 
     // Result = [2, 4]
    
  3. Array.reduce() : This method is one of the most useful ones to perform mathematical operations on a specified array and "reduce" the same in each iteration to return a single, accumulated result. For example :

     const numArr = [1, 2, 3, 4, 5];
     const total = numArr.reduce((sum, item) => sum + item, 0); 
     // Result = 15
    
  4. Array.includes() : This method is used to determine whether a particular element is present in an array or not. This returns a Boolean value (True or False). For example :

     const fruits = ['apple', 'banana', 'orange'];
     const check = fruits.includes('banana'); 
     // Result = true
    
  5. Array.find() : This method is used to find the first element of an array which meets the criteria specified in the method. For example :

     const numArr = [1, 2, 3, 4, 5];
     const firstEven = numArr.find(item => item % 2 === 0); 
     // Result = 2
    
  6. String.replace() & String.replaceAll() : This method finds a particular substring in a string and replaces the substring with another substring - but ONLY the first occurrence. To replace all the occurrences, String.replaceAll() method is used. Note that both the functions can be used with regular expressions as well. For example :

     const text = "I love dogs because dogs are so cute and fluffy!";
     const newText = text.replace("dogs", "rabbits");
     // Result = "I love rabbits because dogs are so cute and fluffy!"
     const totalReplace = text.replaceAll("dogs", "rabbits");
     // Result = "I love rabbits because rabbits are so cute and fluffy!"
    
  7. String.trim() : This method is used to remove any white spaces around a given string. This comes in very handy while performing operations on user input by removing unwanted whitespace. For example :

     const text = "   Hello World   ";
     const trimText = text.trim(); 
     // Result = "Hello World"
    
  8. Math.random() : This method generates a random fraction with a value between 0 and 1 (including 0 but excluding 1). This comes in very handy in a range of operations where random behaviour is required. For example :

     const randomNum = Math.random()
     // e.g. => Result = 0.34896341
    

Note that these are only a few examples of built-in functions used is JS. There are many more methods in JS and do check out the MDN documentation for reference. The more you code, the more you get used to the functions and their utilities. So, let's code!