Working of keywords:
Sometimes it can be helpful to pass a value into the function, but not explicitly declare that variable. In this case, we can use arguments in order to do so! This is where things get interesting.
Use String.prototype to merge with other strings
Use String.prototype to format numbers
Use Array.forEach() instead of loops
It’s also more readable and easier to follow than standard for loops. This code example shows how we can use the built-in Array .forEach() function which was introduced in ECMAScript 2015 (ES2015) instead of a regular loop.
Use String.prototype to get random numbers from arrays
One last trick with strings is that you can actually create random number generators by using them as part of an array or object literal. So even though these aren’t truly “random”, they do have some pretty impressive results! Check out this snippet below where it uses Number(). But could easily be any constructor if you wanted something different such as Date()
Some of these may require using the chrome browser or firefox debugger tools. If you are not already familiar with them then I recommend that you check it out now as they can be very useful for debugging your code and finding problems such as memory leaks which we will discuss later on. Let’s get started:
Set element focus/blur
Get the index of an element in a jQuery object
Get the parent node for any given element
This example shows how you might use this function to find out which file input field was last clicked on before calling another function with that information. Using setTimeout allows us to give priority to certain events by ensuring that code executes once other functions have completed their tasks first.
Retrieve data from one or more inputs at runtime
Here’s some simple code showing how we could collect all text values entered into an input field. We are using the HTMLInputElement type here to represent any element that submits text. But if you wanted to collect other types of data then it’s likely that there will be a corresponding jQuery object for each individual element which you can use instead. We could then later use that data to do whatever it is we want, such as using the value of the first input field in our form for a specific purpose.