JS snippets various (tips & tricks)

JS snippets various (tips & tricks)

Borrowing a method from another object

The methods call, apply and bind are used to define the meaning of the reserved word this, however this also allows us that by changing the meaning of “this” an object can “borrow” a method from another object, as in the following example:

It is often the case that one of the methods of Array that are built into its prototype object is borrowed.

In this way, we borrowed Array’s slice method to some object.

Converting array like objects to string

js this

In JavaScript, there are “array-like” objects, which look like arrays but are not arrays (eg NodeList). Such objects do not have all the methods that a standard array has, e.g. slice, concat, sort, reduce, map, filter… There may also be a need to transfer the arguments object that stores the arguments of the function to an array or even to transfer a string to an array. Converting “array like” objects into a standard string can be done in several ways:

We can easily convert an

“Array-like” object into an array with the slice() method. But the slice() method is not available, so we have to use the call() method to “borrow” the slice() method from arrays. So the final expression would look like this:

If there is a lot of manipulation with DOM and arrayLike objects, it is good to create a function, and call it whenever necessary:

Using the from() function which easily converts all array-like elements to a standard array

  • arrayLike – the name of the element to be transferred to the array
  • someMapFunction – An optional Map function that can be called for each element of the array
  • thisArg – the value to which the this keyword will point within the map function

Using the jQuery.makeArray() function we simply pass the array-like
objects into an array:

Checking the variable with “!!” operator

If there is a need to check if a variable exists and has a valid value, the easiest way is to use the expression !!variable. This expression automatically converts to a boolean value, and if the value of the variable is undefined, “”, 0, null, or NaN it returns FALSE, otherwise it returns TRUE. The same effect is obtained using the Boolean(el).

function

Example

Hide images with error

Targeting images with errors is possible thanks to the fact that the browser fires an error event when it cannot find an image.

Sticky header

See the Pen Sticky Title by Web programming (@chos) on CodePen.

NOTE:
The classList object is not supported by IE for versions less than IE10

Scroll to the top

HTML

JavaScript

Browser detection

Effects: fadeOut & fadeIn

Example

See the Pen FadeOut/FadeIn effect by Web programming (@chos) on CodePen.

Example

See the Pen Fadeout/Fadein jQuery by Web programming (@chos) on CodePen.


Replace part of the text in the string

Replacing part of the text in a string is done with the string method replace(). This method returns a new string with the replaced parts:

regex is either a literal or a RegExp constructor that creates a regular expression pattern. The most common flags used are:

  • g – globally finds all matches (if there is none, the search stops when it finds the first match!)
  • i – ignores case
Example

String search

The String.prototype.indexOf() method returns the index of the first found string within the string, the optional second attribute defines the index from which to start the search.

If the requested string is not found, the method returns the value -1. Please note that this function is case sensitive.. See more about this method in the documentation at MDN developer.mozilla

If two attributes are used in the indexOf() method, then the second attribute defines from which index to start the search.

Example

Example: filtering a list

See the Pen Filter list by Web programming (@chos) on CodePen.

Rounding a decimal number

JavaScript has a very strange approach to decimal numbers:

Due to such unwanted results, it is necessary to round the number.

This function is most similar to rounding from school.

Default behavior

By default this method rounds to the nearest integer:

Rounding to a decimal

If we want to round to decimals, we need to use the trick:

Advanced version rounding to decimals

For rounding to a decimal, use the following function that accepts as arguments the number to be rounded and the number of decimals to which we want to round.

However, this method also has a drawback for the case:

This method returns the first smaller integer of the provided number

This method returns the first larger integer of the provided number:

This method returns an integer by removing the decimals:

Random number generation

Example

Date printing

a) Format: date only

b) Format: date and time

c) Format – date with name of day and month

Is it a leap year?

Prevention of multiple form submissions

See the Pen xXyRwZ by Web programming (@chos) on CodePen.