## ES6 – Convert To Integer

There are various ways to convert a string or a floating point number to an integer in JavaScript, but not all of them may suit your needs. In this post we will examine four different ways and then we will see which is the best in all cases.

### Solution #1: `~~x`

If we are dealing with smaller number then we can leverage bit-wise operators such as bit-wise negation (`~`). The reason we can only use this solution for smaller numbers is because bit-wise operations can only be executed on a number in the range of `-(231)` to `231 - 1`. If the number is outside of that range it will be converted to a number within that range (eg. `-2147483649` becomes `2147483647` and `2147483648` becomes `-2147483648`).

### Solution #2: `parseInt(x, 10)`

There is actually a function that will convert strings into numbers called `parseInt()`. We can use this function and specify the base to ensure that numbers starting with `0` will not be parsed as octals (eg. `parseInt('010')` produces `8` in older versions of ECMAScript).

### Solution #3: `+parseFloat(x, 10).toFixed(0)`

We can also use the equivalent function to `parseInt()` for floats which is `parseFloat()`. After that we can use `Number.prototype.toFixed()` to make the number a string representation of the truncated number. Finally we can prefix this expression with a plus sign (`+`) to convert the string to a number again.

### Solution #4: `Math.trunc(x)`

We can use the newer `Math.trunc()` function which should in theory just do what we want ðŸ˜† .

### Results

Below are the results of using your browser to test out each solution:

### Conclusion

If you have looked through the results above on a modern browser (one implementing `Math.trunc()`) you’ll notice that for all of our test cases, it is the only solution that works for them all. Therefore, if you are looking to convert floating-point numbers to integers or strings to integers, the most convenient and straightforward way may in fact be ES6’s new `Math.trunc()` function. Let me know what you think and as always, happy coding! ðŸ˜Ž

## JavaScript Snippet – isValidVarName()

Now Available in YourJS

Recently I was working on a function which needed to determine whether or not a string could be used as a variable name. Variable name validation can get tricky so instead of using a crazy regular expression which includes all keywords (which may change over time) and instead of testing for strange unicode characters which are rarely used for variable names, I decided to leverage the JavaScript Function constructor:

The above function takes the string in question and returns `true` if the string can be used a variable name. If the string can not be used as a variable name `false` is returned.

Some may wonder why I’m doing the following:

```varName.replace(/[\s\xA0,\/]|^\$/g, '.')
```

The reason I included the above replacement is to avoid false-positives in the case of an empty string, extra spacing, commas, and forward slashes.

### Security

Others have attempted to make the same function using the evil `eval()` function which allows for JS injection. Even though the `Function` constructor can also be used for evil, when supplying arguments it does prevent you from doing JS injection by making sure the arguments don’t have parentheses.

### Examples

The following is an example of what will happen when running for the function for a number of strings:

```console.log(isValidVarName(''));           // -> false
console.log(isValidVarName('3'));           // -> false
console.log(isValidVarName('3d'));          // -> false
console.log(isValidVarName('D3'));          // -> true
console.log(isValidVarName('D3 '));         // -> false
console.log(isValidVarName('D3,Q'));        // -> false
console.log(isValidVarName('D3/*Qs*/'));   // -> false
console.log(isValidVarName('D3Q'));         // -> true
console.log(isValidVarName('var'));         // -> false
console.log(isValidVarName('true'));        // -> false
console.log(isValidVarName('undefined'));   // -> true
console.log(isValidVarName('null'));        // -> false
console.log(isValidVarName('coolio.pop'));  // -> false
console.log(isValidVarName('coolio'));      // -> true
console.log(isValidVarName('coolio_pop'));  // -> true
console.log(isValidVarName('\$'));           // -> true
console.log(isValidVarName('\$Ã¡'));          // -> true
console.log(isValidVarName('Ã¡Ã‘'));          // -> true
console.log(isValidVarName('_'));           // -> true
```

Here is a similar example hosted on JSBin:
JS Bin on jsbin.com

## YourJS – 1st Pass

I finally have the 1st pass of YourJS available to the public!!!

## What is YourJS?

YourJS is a fully customizable JavaScript library which gives the developer more control than ever before.

## Who controls the codebase?

Everyone does! All you have to do is sign up and you can start writing code that can be included in YourJS.