JS Array push and other ways of adding elements to an array

So in javaScript there is the array push prototype method that is typically what is used as a way to push new elements to the end of a javaScript array. There are many other ways of going about adding elements to an array also though, such as just using the object bracket syntax, as well as a range of other methods.

So I thought I would write a quick post on this when it comes to the basics of adding elements to an array in javaScript. This is after all a major part of working with arrays in javaScript so it is a good idea to get what the options are fairly solid.

Read More

JS Array map method examples

It is a common task in javaScript projects to need to loop over the full contents of an array, and create some sort of product for each element in that array. There are methods like the Array for each method that can be used to do this sort of thing, along with other features in javaScript such as just doing such things with loops and the array bracket syntax. However there is an array prototype method that each javaScript developer should be aware of called array map.

I have wrote a post a long while back on the lodash map method that works more or less the same way as array map only it will work on most objects in general rather than just arrays. That of course is yet another option to be aware of if you are working on a project where lodash is part of the stack. However in this post I will be focusing more so on the native array map prototype method, as well as little tips and tricks that have to do with getting array map to work with objects in general as well as other native javaScriot examples for doing the same thing without array map.

Read More

JS Math round method and precision

In javaScript there is the Math object and a few of the many methods in this Object have to do with rounding numbers such as Math ceil, Math floor, and one additional such option for rounding in the Math Object that is the Math round method. For the most part these methods will work just fine, however there are some situations in which they might fall short for expectations. One situation that comes to mind has to do with precession, which is one of several things that come to mind that might make one want to have a custom user space solution for rounding.

In the utility library lodash there is a _.round method that works more or less the same way as the native Math round method, but precession can be set by way of a second argument that is absent from the native Math round method. This lodash method alone is not a great talking point for using the whole utility library though, so it is often desirable to have just a quick, simple copy and past solution for this sort of thing which it comes to working with native javaScript by itself.

So in this post I will be taking a look at the Math.round method, but also additional options, user space alternatives, and any related topics that might come up when it comes to formatting numbers for the sake of presentation.

Read More

JS Array of method for creating an array from arguments

So in late specs of javaScript there is a native Array.of static method in the array object that can be used to create an array of elements from arguments that are passed when calling the array of method.

It would seem that this method was introduced as a way to provide something that is missing when using the Array constructor directly with one argument. That is calling the main Array constructor method with the new keyword as a way to create a new instance of an array, rather than using the bracket syntax to do so. When using the Array constructor with new there is using just one optional argument that is a number rather than some other value when doing so this sets a starting length of the array, but not the first value of the array.

However often new javaScript developers find the use of the Array constructor with one argument a little confusing and assume that a starting value for the first element can be passed to the constructor as the first argument. So then this can cause some confusion with new developers that are not familiar with the fact that a starting value for the first elemnt can not be set that way. So the Array of method is now a way to create a new array by passing some arguments for the starting element values for the array rather than just a starting length, and works as expected when passing just one argument that is a number.

I can not say that I use the Array of method often, as I prefer to use some of the older tired yet true ways of doing the same thing that is just a little more involved. But never the less this post will be on the JS array of meto9d and other ways of creating a new array with a set number of starting values.

Read More

Game Spinner Canvas example

This canvas example will be of a game spinner. In other words a virtual from of one of those things that you get in many board games that functions as an alternative to dice that has a spinner or arrow type thing attached to the center of a disk with a bunch of sections on it. So this canvas example will involve a module that can be used to create a state object for this sort of thing, and like aways a draw module that is used to draw the state of one of these to a canvas element.

Read More