Skip to main content
Stack Overflow
  1. About
  2. For Teams

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

Required fields*

Convert character to ASCII code in JavaScript

How can I convert a character to its ASCII code using JavaScript?

For example:

get 10 from "\n".

Answer*

Draft saved
Draft discarded
Cancel
13
  • 821
    The opposite of this is String.fromCharCode(10). Commented May 1, 2011 at 9:38
  • 227
    Fun fact: you don’t really need the 0 (first argument value) — just "\n".charCodeAt() will do. Commented Oct 17, 2011 at 9:40
  • 54
    @MathiasBynens: and fortunately this is documented: developer.mozilla.org/en/JavaScript/Reference/Global_Objects/…. "if it is not a number, it defaults to 0" Commented Nov 15, 2011 at 19:46
  • 14
    You should point out that unlike String.fromCharCode( asciiNumVal ), stringInstance.charCodeAt( index ) is not a static method of class String Commented Sep 12, 2012 at 19:09
  • 29
    @Mathias Bynens, It certainly does default to zero but I just ran a just out of interest test on performance and it performs **relatively badly compared using 0. jsperf.com/default-to-0-vs-0/4 ** Its a relative difference only, either way its very very quick. Commented May 9, 2013 at 12:35

lang-js

AltStyle によって変換されたページ (->オリジナル) /