Asc() and Chr() in JavaScript
I have mostly coded in languages in VB family i.e. VB, VBA, VBScript, ASP. So when I needed to code in JavaScript, I was left with no choice but to Google for JavaScript equivalents for Asc() and Chr() functions, which I badly required to use for my assignment. And what I came up with is as described below.
The Asc() and Chr() functions as exist with VB languages are available as resp. charCodeAt() and fromCharCode() methods of string object.
While Asc() function in VBScript returns ASCII value for the first character of the string, charCodeAt method takes for parameter an index and returns ASCII code for character at given index in the string. So if you need to get ASCII value for the first character just pass 0 for the parameter.
If you still think that you prefer VB style, why not code functions that work and return values similar to VB functions. This will also illustrate the usage of the equivalent JavaScript functions.
<script>
function Asc(String)
{
return String.charCodeAt(0);
}
function Chr(AsciiNum)
{
return String.fromCharCode(AsciiNum)
}
</script>
On Technorati :
JavaScript



[...] lastly, and most surprisingly Asc() and Chr() in JavaScript is continuesly leaving other posts behind from the day it was [...]
Last 30 days « Jalaj
2 Apr 07 at 11:36 am
[...] Asc() and Chr() in JavaScript [...]
It’s 6 months since this blog started « Jalaj
12 Jun 07 at 8:18 am
[...] knew this was a question that’s frequently searched on Internet. It was after that I posted Asc() and Chr() in JavaScript that I came to know about it looking at the number of hits that it [...]
The Blog Revisited - 6 « Jalaj
31 Oct 07 at 8:23 am
[...] of cross-browser compatibility. One of such developers is me, myself. First it was my search for Asc() and Chr() in JavaScript and now I take here INT() and VAL() functions. Lets start with code if you want in VB [...]
INT() and VAL() in JavaScript « Jalaj
29 Nov 07 at 4:36 am
[...] visits but yes there are people up there who do require such trivial information. These posts were Asc() and Chr() in JavaScript, Convert PPS to PPT and How Many MegaPixels is a VGA [...]
Year One Digest « Jalaj
12 Dec 07 at 2:39 pm
Hey, thank you! I’m a VB user trying to understand JavaScript
Zim
7 May 09 at 7:41 pm