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.
Related Posts:
- Declaring multiple variables in JavaScript
- How can I extract a number from a string in JavaScript?
- How to check whether a string contains a substring in JavaScript?
- Check if a string has a certain piece of text [duplicate]
- How to convert a string to an integer in JavaScript?
- How can I do string interpolation in JavaScript?
- How do I make the first letter of a string uppercase in JavaScript?
- How do I make the first letter of a string uppercase in JavaScript?
- Creating multiline strings in JavaScript
- Creating multiline strings in JavaScript
- Split string into array
- How do you reverse a string in-place in JavaScript?
- What is the correct way to check for string equality in JavaScript?
- Check if a variable is a string in JavaScript
- Check if a variable is a string in JavaScript
- How can I remove a character from a string using JavaScript?
- What is the difference between call and apply?
- How to print elements from array with javascript
- How to remove text from a string?
- What does ${} (dollar sign and curly braces) mean in a string in Javascript?
- Delete first character of a string in Javascript
- ES6: Create Strings using Template Literals – Freecodecamp
- How to print a number with commas as thousands separators in JavaScript
- How do I check if string contains substring? [duplicate]
- How do I check if string contains substring?
- How can I get the last character in a string?
- How can I get the last character in a string?
- Count the number of occurrences of a character in a string in Javascript
- Converting an object to a string
- How do I convert an integer to binary in JavaScript?
- Anagrams finder in javascript
- How to interpolate variables in strings in JavaScript, without concatenation?
- String.Format not work in TypeScript
- How to do case insensitive string comparison?
- How to remove part of a string?
- Escaping HTML strings with jQuery
- Check if string contains only digits
- Replace all spaces in a string with ‘+’
- How to sort an array of integers correctly
- what is ScriptResource.axd in the page load in web app?
- How to count string occurrence in string?
- Insert a string at a specific index
- Strip HTML from Text JavaScript
- Strip all non-numeric characters from string in JavaScript
- Wildcard string comparison in Javascript
- Replace spaces with dashes and make all letters lower-case
- Check if a JavaScript string is a URL
- How to use Head JS with all enqueued scripts?
- How to combine javascript files without plugins?
- wp_register_script multiple identifiers?
- Only loads on the contact template page
- Preloader for a WordPress Site
- Performance vs plugin genrated javascript
- How can I integrate yepnope.js with WordPress (and plugins)?
- window.location.href and window.open () methods in JavaScript
- Define a global variable in a JavaScript function
- Can’t bind to ‘ngModel’ since it isn’t a known property of ‘input’
- Cross-Origin Read Blocking (CORB)
- Javascript Uncaught SyntaxError: Unexpected identifier
- “document.getElementByClass is not a function”
- How can I create a two dimensional array in JavaScript?
- How can I refresh a page with jQuery?
- Remove duplicate values from JS array
- Add days to JavaScript Date
- ajax jquery simple get request
- Generate random string/characters in JavaScript
- How to resolve TypeError: Cannot convert undefined or null to object
- What is prevState in ReactJS? [duplicate]
- React with ES7: Uncaught TypeError: Cannot read property ‘state’ of undefined
- Difference between textContent vs innerText
- How can I guarantee that my enums definition doesn’t change in JavaScript?
- How to write an inline IF statement in JavaScript?
- ‘setInterval’ vs ‘setTimeout’
- Uncaught TypeError: Cannot read property ‘top’ of undefined
- onClick to get the ID of the clicked button
- HTML text input allow only numeric input
- How to change the buttons text using javascript
- document.getElementbyId() returning null
- Seeding the random number generator in Javascript
- Bootstrap popover is not working
- Sort array of objects by single key with date value
- How to add a tooltip to an svg graphic?
- Complex JSON nesting of objects and arrays
- jQuery – Increase the value of a counter when a button is clicked
- AngularJS POST Fails: Response for preflight has invalid HTTP status code 404
- Getting “Cannot call a class as a function” in my React Project
- Separating html and JavaScript in Flask [duplicate]
- How to read and write into file using JavaScript?
- How to use aria-expanded=”true” to change a css property
- Nonce retrieved from the REST API is invalid and different from nonce generated in wp_localize_script
- wp.customize.bind ready event not fired
- Load script after block is inserted
- How can I use CKEditor with WordPress and let clients edit the content?
- Gutenberg getBlockIndex in save() function
- Widgets and Post/Page edit/new blank, console errors only, no server errors
- Custom JS text area in customizer is being formatted wrong in document
- how to prevent added meta box from being injected with style=”position: fixed; top: 56px;” at scroll down
- How to use value from modal tinymce windowManager?
- early enqueueing javascript file in page template, not in functions.php
- Wysiwyg editor not working on 4.2.2