December 29, 2022
Estimated Post Reading Time ~

100+ JavaScript Snippets for Beginners

100+ JavaScript Snippets for Beginners

1. all

This snippet returns **true**if the predicate function returns true for all elements in a collection and **false**otherwise. You can omit the second argument fn if you want to use Boolean as a default value.



2. allEqual

This snippet checks whether all elements of the array are equal.



3. approximatelyEqual

This snippet checks whether two numbers are approximately equal to each other, with a small difference.



4. arrayToCSV

This snippet converts the elements that don’t have commas or double quotes to strings with comma-separated values.



5. arrayToHtmlList

This snippet converts the elements of an array into `` tags and appends them to the list of the given ID.



6. attempt

This snippet executes a function, returning either the result or the caught error object.



7. average

This snippet returns the average of two or more numerical values.



8. averageBy

This snippet returns the average of an array after initially doing the mapping of each element to a value using a given function.



9. bifurcate

This snippet splits values into two groups and then puts a truthy element of **filter**in the first group, and in the second group otherwise.

You can use **Array.prototype.reduce()**and **Array.prototype.push()**to add elements to groups based on filter.



10. bifurcateBy

This snippet splits values into two groups, based on a predicate function. If the predicate function returns a truthy value, the element will be placed in the first group. Otherwise, it will be placed in the second group.

You can use **Array.prototype.reduce()**and **Array.prototype.push()**to add elements to groups, based on the value returned by **fn**for each element.



11. bottomVisible

This snippet checks whether the bottom of a page is visible.



12. byteSize

This snippet returns the length of a string in bytes.



13. capitalize

This snippet capitalizes the first letter of a string.



14. capitalizeEveryWord

This snippet capitalizes the first letter of every word in a given string.



15. castArray

This snippet converts a non-array value into array.



16. compact

This snippet removes false values from an array.



17. countOccurrences

This snippet counts the occurrences of a value in an array.



18. Create Directory

This snippet uses existsSync() to check whether a directory exists and then mkdirSync() to create it if it doesn’t.



19. currentURL

This snippet returns the current URL.



20. dayOfYear

This snippet gets the day of the year from a **Date**object.



21. decapitalize

This snippet turns the first letter of a string into lowercase.



22. deepFlatten

This snippet flattens an array recursively.



23. default

This snippet assigns default values for all properties in an object that are undefined.



24. defer

This snippet delays the execution of a function until the current call stack is cleared.



25. degreesToRads

This code snippet can be used to convert a value from degrees to radians.



26. difference

This snippet finds the difference between two arrays.



27. differenceBy

This method returns the difference between two arrays, after applying a given function to each element of both lists.



28. differenceWith

This snippet removes the values for which the comparator function returns false.



29. digitize

This snippet gets a number as input and returns an array of its digits.



30. distance

This snippet returns the distance between two points by calculating the Euclidean distance.



31. Drop Elements

This snippet returns a new array with n elements removed from the left.



32. dropRight

This snippet returns a new array with n elements removed from the right.



33. dropRightWhile

This snippet removes elements from the right side of an array until the passed function returns true.



34. dropWhile

This snippet removes elements from an array until the passed function returns true.



35. elementContains

This snippet checks whether the parent element contains the child.



36. Filter Duplicate Elements

This snippet removes duplicate values in an array.



37. findKey

This snippet returns the first key that satisfies a given function.



38. findLast

This snippet returns the last element for which a given function returns a truthy value.



39. flatten

This snippet flattens an array up to a specified depth using recursion.



40. forEachRight

This snippet executes a function for each element of an array starting from the array’s last element.



41. forOwn

This snippet iterates on each property of an object and iterates a callback for each one respectively.



42. functionName

This snippet prints the name of a function into the console.



43. Get Time From Date

This snippet can be used to get the time from a **Date**object as a string.



44. Get Days Between Dates

This snippet can be used to find the difference in days between two dates.



45. getStyle

This snippet can be used to get the value of a CSS rule for a particular element.



46. getType

This snippet can be used to get the type of a value.



47. hasClass

This snippet checks whether an element has a particular class.



48. head

This snippet returns the head of a list.



49. hide

This snippet can be used to hide all elements specified.



50. httpsRedirect

This snippet can be used to redirect from HTTP to HTTPS in a particular domain.



51. indexOfAll

This snippet can be used to get all indexes of a value in an array, which returns an empty array, in case this value is not included in it.


52. initial

This snippet returns all elements of an array except the last one.



53. insertAfter

This snippet can be used to insert an HTML string after the end of a particular element.



54. insertBefore

This snippet can be used to insert an HTML string before a particular element.



55. intersection

This snippet can be used to get an array with elements that are included in two other arrays.



56. intersectionBy

This snippet can be used to return a list of elements that exist in both arrays, after a particular function has been executed to each element of both arrays.



57. intersectionWith

This snippet can be used to return a list of elements that exist in both arrays by using a comparator function.



.

58. is

This snippet can be used to check if a value is of a particular type.



59. isAfterDate

This snippet can be used to check whether a date is after another date.



60. isAnagram

This snippet can be used to check whether a particular string is an anagram with another string.



61. isArrayLike

This snippet can be used to check if a provided argument is iterable like an array.



62. isBeforeDate

This snippet can be used to check whether a date is before another date.



63. isBoolean

This snippet can be used to check whether an argument is a boolean.



64. isBrowser

This snippet can be used to determine whether the current runtime environment is a browser. This is helpful for avoiding errors when running front-end modules on the server (Node).



65. isBrowserTabFocused

This snippet can be used to determine whether the browser tab is focused.



66. isLowerCase

This snippet can be used to determine whether a string is lower case.



67. isNil

This snippet can be used to check whether a value is null or undefined.



68. isNull

This snippet can be used to check whether a value is null.



69. isNumber

This snippet can be used to check whether a provided value is a number.



70. isObject

This snippet can be used to check whether a provided value is an object. It uses the Object constructor to create an object wrapper for the given value.

If it is already an object, then an object type that corresponds to the given value will be returned. Otherwise, a new object will be returned.



71. isObjectLike

This snippet can be used to check if a value is not **null**and that its typeof is “object”.



72. isPlainObject

This snippet checks whether a value is an object created by the Object constructor.



73. isPromiseLike

This snippet checks whether an object looks like a Promise.



74. isSameDate

This snippet can be used to check whether two dates are equal.



75. isString

This snippet can be used to check whether an argument is a string.



76. isSymbol

This snippet can be used to check whether an argument is a symbol.



77. isUndefined

This snippet can be used to check whether a value is undefined.



78. isUpperCase

This snippet can be used to check whether a string is upper case.



79. isValidJSON

This snippet can be used to check whether a string is a valid JSON.



80. last

This snippet returns the last element of an array



81. matches

This snippet compares two objects to determine if the first one contains the same property values as the second one.



82. maxDate

This snippet can be used to get the latest date.



83. maxN

This snippet returns the n largest elements from a list. If **n**is greater than or equal to the list’s length, then it will return the original list (sorted in descending order).



84. minDate

This snippet can be used to get the earliest date.



85. minN

This snippet returns the n smallest elements from a list. If **n**is greater than or equal to the list’s length, then it will return the original list (sorted in ascending order).



86. negate

This snippet can be used to apply the not operator (!) to a predicate function with its arguments.



87. nodeListToArray

This snippet can be used to convert a nodeList to an array.



88. pad

This snippet can be used to pad a string on both sides with a specified character if it is shorter than the specified length.



89. radsToDegrees

This snippet can be used to convert an angle from radians to degrees.



90. Random Hexadecimal Color Code

This snippet can be used to generate a random hexadecimal color code.



91. randomIntArrayInRange

This snippet can be used to generate an array with n random integers in a specified range.



92. randomIntegerInRange

This snippet can be used to generate a random integer in a specified range.



93. randomNumberInRange

This snippet can be used to return a random number in a specified range.



94. readFileLines

This snippet can be used to read a file by getting an array of lines from a file



95. Redirect to a URL

This snippet can be used to do a redirect to a specified URL.



96. reverse

This snippet can be used to reverse a string.



97. round

This snippet can be used to round a number to a specified number of digits.



98. runPromisesInSeries

This snippet can be used to run an array of promises in series.



99. sample

This snippet can be used to get a random number from an array.



100. sampleSize

This snippet can be used to get **n**random elements from unique positions from an array up to the size of the array. Elements in the array are shuffled using the Fisher-Yates algorithm



101**. findKey**

This snippet returns the first key that satisfies a given function.



102**. findLast**

This snippet returns the last element for which a given function returns a truthy value.



103**. flatten**

This snippet flattens an array up to a specified depth using recursion.



104**. forEachRight**

This snippet executes a function for each element of an array starting from the array’s last element.



105. forOwn

This snippet iterates on each property of an object and iterates a callback for each one respectively.



106**. functionName**

This snippet prints the name of a function into the console.



Thank you for reading!!


By aem4beginner

No comments:

Post a Comment

If you have any doubts or questions, please let us know.