like this:

var foo = 45;
var bar = '' + foo;

Actually, even though I typically do it like this for simple convenience, over 1,000s of iterations it appears for raw speed there is an advantage for .toString()

See Performance tests here (not by me, but found when I went to write my own): http://jsben.ch/#/ghQYR

Fastest based on the JSPerf test above: str = num.toString();

It should be noted that the difference in speed is not overly significant when you consider that it can do the conversion any way 1 Million times in 0.1 seconds.

Update: The speed seems to differ greatly by browser. In Chrome num + '' seems to be fastest based on this test http://jsben.ch/#/ghQYR

Update 2: Again based on my test above it should be noted that Firefox 20.0.1 executes the .toString() about 100 times slower than the '' + num sample.

Answer from scunliffe on Stack Overflow
🌐
W3Schools
w3schools.com › jsref › jsref_tostring_number.asp
W3Schools.com
Function apply() Function bind() Function call() Function length Function name Function toString() Function valueOf() JS Error · new Error() cause isError() name message JS Global · decodeURI() decodeURIComponent() encodeURI() encodeURIComponent() escape() eval() Infinity isFinite() isNaN() NaN Number() parseFloat() parseInt() String() undefined unescape() JS Iterators
People also ask

How to get only int value from string in JavaScript?
Use parseInt() to extract the leading integer from a string. If the string contains other characters, parseInt() will stop parsing when it encounters a non-numeric character.
🌐
upgrad.com
upgrad.com › home › tutorials › software & tech › convert string to int in javascript
Convert String to int in JavaScript
How to convert string to int without using parseInt JavaScript?
You can use the number() constructor which carries out JavaScript convert string to integer (integer or float if decimals are present). You can also use the unary plus operator (+) which converts string to a number type.
🌐
upgrad.com
upgrad.com › home › tutorials › software & tech › convert string to int in javascript
Convert String to int in JavaScript
How to check if string can be converted to int JavaScript?
Use the isNaN() function in combination with parseInt(). If isNaN(parseInt(yourString)) returns true, the string can't be reliably converted to an integer.
🌐
upgrad.com
upgrad.com › home › tutorials › software & tech › convert string to int in javascript
Convert String to int in JavaScript
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Number › toString
Number.prototype.toString() - JavaScript | MDN
The toString() method of Number values returns a string representing this number value. function hexColor(c) { if (c < 256) { return Math.abs(c).toString(16); } return 0; } console.log(hexColor(233)); // Expected output: "e9" console.log(hexColor("11")); // Expected output: "b" ... An integer ...
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › parseInt
parseInt() - JavaScript | MDN
If not NaN, the return value will be the integer that is the first argument taken as a number in the specified radix. (For example, a radix of 10 converts from a decimal number, 8 converts from octal, 16 from hexadecimal, and so on.) The radix argument is converted to a number. If it's unprovided, or if the value becomes 0, NaN or Infinity (undefined is coerced to NaN), JavaScript assumes the following: If the input string, with leading whitespace and possible +/- signs removed, begins with 0x or 0X (a zero, followed by lowercase or uppercase X), radix is assumed to be 16 and the rest of the string is parsed as a hexadecimal number.
🌐
Built In
builtin.com › articles › javascript-string-to-a-number
How to Convert a JavaScript String to Number | Built In
Number() can be used to convert JavaScript variables to numbers. We can also use it to convert the string into a number.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Number
Number - JavaScript | MDN
Integers can only be represented without loss of precision in the range -253 + 1 to 253 - 1, inclusive (obtainable via Number.MIN_SAFE_INTEGER and Number.MAX_SAFE_INTEGER), because the mantissa can only hold 53 bits (including the leading 1). More details on this are described in the ECMAScript standard. Many built-in operations that expect numbers first coerce their arguments to numbers (which is largely why Number objects behave similarly to number primitives). The operation can be summarized as follows: Numbers are returned as-is. ... Strings are converted by parsing them as if they contain a number literal.
Find elsewhere
🌐
Scaler
scaler.com › home › topics › convert string to number in javascript
String to Number Javascript | Scaler Topics
January 6, 2024 - In this article, we will learn How to convert string to number javascript input in Javascript.
Top answer
1 of 16
3113

The simplest way would be to use the native Number function:

Copyvar x = Number("1000")

If that doesn't work for you, then there are the parseInt, unary plus, parseFloat with floor, and Math.round methods.

parseInt()

Copyvar x = parseInt("1000", 10); // You want to use radix 10
    // So you get a decimal number even with a leading 0 and an old browser ([IE8, Firefox 20, Chrome 22 and older][1])

Unary plus

If your string is already in the form of an integer:

Copyvar x = +"1000";

floor()

If your string is or might be a float and you want an integer:

Copyvar x = Math.floor("1000.01"); // floor() automatically converts string to number

Or, if you're going to be using Math.floor several times:

Copyvar floor = Math.floor;
var x = floor("1000.01");

parseFloat()

If you're the type who forgets to put the radix in when you call parseInt, you can use parseFloat and round it however you like. Here I use floor.

Copyvar floor = Math.floor;
var x = floor(parseFloat("1000.01"));

round()

Interestingly, Math.round (like Math.floor) will do a string to number conversion, so if you want the number rounded (or if you have an integer in the string), this is a great way, maybe my favorite:

Copyvar round = Math.round;
var x = round("1000"); // Equivalent to round("1000", 0)
2 of 16
306

Try parseInt function:

Copyvar number = parseInt("10");

But there is a problem. If you try to convert "010" using parseInt function, it detects as octal number, and will return number 8. So, you need to specify a radix (from 2 to 36). In this case base 10.

CopyparseInt(string, radix)

Example:

Copyvar result = parseInt("010", 10) == 10; // Returns true

var result = parseInt("010") == 10; // Returns false

Note that parseInt ignores bad data after parsing anything valid.
This guid will parse as 51:

Copyvar result = parseInt('51e3daf6-b521-446a-9f5b-a1bb4d8bac36', 10) == 51; // Returns true
🌐
TypeScript
typescriptlang.org › docs › handbook › 2 › everyday-types.html
TypeScript: Documentation - Everyday Types
Now that we know how to write a few types, it’s time to start combining them in interesting ways. The first way to combine types you might see is a union type. A union type is a type formed from two or more other types, representing values that may be any one of those types. We refer to each of these types as the union’s members. Let’s write a function that can operate on strings or numbers:
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › BigInt
BigInt - JavaScript | MDN
A BigInt value, also sometimes just called a BigInt, is a bigint primitive, created by appending n to the end of an integer literal, or by calling the BigInt() function (without the new operator) and giving it an integer value or string value.
🌐
freeCodeCamp
freecodecamp.org › news › string-to-number-in-javascript-convert-a-string-to-an-int-in-js
String to Number in JavaScript – Convert a String to an Int in JS
August 30, 2022 - In this article, you will learn some of the built-in JavaScript methods available for converting strings to numbers, along with an introduction (or refresher!) to the basics of how strings and numbers work in JavaScript.
🌐
egghead.io
egghead.io › lessons › typescript-parse-a-string-to-an-integer
Parse a string to an integer | egghead.io
A common interview question is to write a function that converts a string into an integer e.g. '123' => 123. This function is commonly called ato...
Published   July 27, 2017
🌐
JavaScript.info
javascript.info › tutorial › the javascript language › javascript fundamentals
Data types
For example, a variable can at one moment be a string and then store a number: // no error let message = "hello"; message = 123456; Programming languages that allow such things, such as JavaScript, are called “dynamically typed”, meaning that there exist data types, but variables are not bound to any of them. ... The number type represents both integer ...
🌐
DEV Community
dev.to › himanshudevgupta › 7-ways-to-convert-a-string-to-number-in-javascript-35pd
7 ways to convert a String to Number in JavaScript - DEV Community
February 21, 2023 - 1. Using parseInt() parseInt() parses a string and returns a whole number. Spaces are allowed. Only the first number is returned. This method has a limitation though. If you parse the decimal number, it will be rounded off to the nearest integer ...