10 Best JavaScript Code Snippets Learn JavaScript Free PDF Guide

Write a function that checks whether two given strings are anagrams in JavaScript.
Write a function that calculates the average of all numbers in an array in JavaScript.
Write a function that converts a string to title case (i.e., the first letter of each word capitalized) in JavaScript.
Write a function that returns the number of vowels in a given string in JavaScript.
Write a function that calculates the factorial of a given number without using recursion in JavaScript.
Write a function that returns the second largest number in an array in JavaScript.
Write a function that checks whether a given string is a pangram (i.e., contains all letters of the alphabet) in JavaScript.
Write a function that calculates the sum of all even numbers in an array in
Write a function that checks whether a given number is a perfect square (i.e., the square of an integer) in JavaScript.
Write a function that converts a temperature in Fahrenheit to Celsius in JavaScript.

10 Helpful JavaScript Code Snippets

Write a function that checks whether two given strings are anagrams in JavaScript.

function isAnagram(str1, str2) {

  const sorted1 = str1.split(“”).sort().join(“”);

  const sorted2 = str2.split(“”).sort().join(“”);

  return sorted1 === sorted2;

}

Explanation: The function first sorts the characters in each input string alphabetically using the sort() method. Then, it checks if the two sorted strings are equal (i.e., they contain the same characters in the same frequency).

Write a function that calculates the average of all numbers in an array in JavaScript.

function average(arr) {

  const sum = arr.reduce((acc, cur) => acc + cur, 0);

  return sum / arr.length;

}

Explanation: The function uses the reduce() method on the input array to accumulate the sum of all numbers in the array. Then, it divides the sum by the length of the array to calculate the average.

Write a function that converts a string to title case (i.e., the first letter of each word capitalized) in JavaScript.

function toTitleCase(str) {

  return str.replace(/\b\w/g, (char) => char.toUpperCase());

}

Explanation: The function uses a regular expression (/\b\w/g) to match the first character of each word in the input string. Then, it uses the replace() method to replace each of these characters with its uppercase version using an arrow function.

Write a function that returns the number of vowels in a given string in JavaScript.

function countVowels(str) {

  const vowels = “aeiou”;

  return str.split(“”).filter((char) => vowels.includes(char)).length;

}

Explanation: The function first defines a string of all vowels. Then, it splits the input string into an array of characters and filters out all non-vowel characters using the filter() method and the includes() method. Finally, it returns the length of the resulting array.

Write a function that calculates the factorial of a given number without using recursion in JavaScript.

function factorial(num) {

  let result = 1;

  for (let i = 1; i <= num; i++) {

    result *= i;

  }

  return result;

}

Explanation: The function uses a for loop to multiply the current result by each integer from 1 to the input number.

Write a function that returns the second largest number in an array in JavaScript.

function secondLargest(arr) {

  const sorted = arr.sort((a, b) => b – a);

  return sorted[1];

}

Explanation: The function first sorts the input array in descending order using the sort() method and a comparator function. Then, it returns the second element of the sorted array (which is the second largest number).

Write a function that checks whether a given string is a pangram (i.e., contains all letters of the alphabet) in JavaScript.

function isPangram(str) {

  const alphabet = “abcdefghijklmnopqrstuvwxyz”;

  const lowercase = str.toLowerCase();

  return alphabet.split(“”).every((char) => lowercase.includes(char));

}

Explanation: The function first defines a string of all letters of the alphabet. Then, it converts the input string to lowercase. Finally, it checks if every letter in the alphabet is included in the lowercase string using the every() method.

Write a function that calculates the sum of all even numbers in an array in

function sumEven(arr) {

  return arr.filter((num) => num % 2 === 0).reduce((acc, cur) => acc + cur, 0);

}

Explanation: The function first uses the filter() method on the input array to filter out all odd numbers (i.e., numbers that have a remainder of 1 when divided by 2). Then, it uses the reduce() method to accumulate the sum of all even numbers in the resulting array.

Write a function that checks whether a given number is a perfect square (i.e., the square of an integer) in JavaScript.

function isPerfectSquare(num) {

  const root = Math.sqrt(num);

  return root === Math.floor(root);

}

Explanation: The function uses the Math.sqrt() method to calculate the square root of the input number. Then, it checks if the square root is equal to its floor (i.e., if it is an integer).

Write a function that converts a temperature in Fahrenheit to Celsius in JavaScript.

function fahrenheitToCelsius(fahrenheit) {

  return (fahrenheit – 32) * 5 / 9;

}

Explanation: The function uses the formula (F – 32) * 5/9 to convert a temperature in Fahrenheit to Celsius.