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
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.
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.
11. bottomVisible
13. capitalize
14. capitalizeEveryWord
This snippet capitalizes the first letter of every word in a given string.
15. castArray
16. compact
17. countOccurrences
This snippet uses existsSync() to check whether a directory exists and then mkdirSync() to
create it if it doesn’t.
19. currentURL
20. dayOfYear
21. decapitalize
22. deepFlatten
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
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.
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.
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
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 can be used to get the time from a **Date**object as a string.
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
47. hasClass
48. head
49. hide
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 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
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
67. isNil
68. isNull
69. isNumber
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
74. isSameDate
This snippet can be used to check whether two dates are equal.
75. isString
76. isSymbol
77. isUndefined
78. isUpperCase
80. last
81. matches
This snippet compares two objects to determine if the first one contains the same property values
as the second one.
82. maxDate
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
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
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
91. randomIntArrayInRange
This snippet can be used to generate an array with n random integers in a specified range.
92. randomIntegerInRange
93. randomNumberInRange
94. readFileLines
This snippet can be used to read a file by getting an array of lines from a file
96. reverse
97. round
98. runPromisesInSeries
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**
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**