Jalaj P. Jha

Technical & Miscellaneous Ramblings

Asc() and Chr() in JavaScript

with 6 comments

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 :

Written by jalaj

March 8, 2007 at 10:39 am

Posted in JavaScript

Tagged with

6 Responses

Subscribe to comments with RSS.

  1. Hey, thank you! I’m a VB user trying to understand JavaScript :P

    Zim

    May 7, 2009 at 7:41 pm

  2. [...] 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

    December 12, 2007 at 2:39 pm

  3. [...] 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 [...]

  4. [...] 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 [...]

  5. [...] Asc() and Chr() in JavaScript [...]

  6. [...] lastly, and most surprisingly Asc() and Chr() in JavaScript is continuesly leaving other posts behind from the day it was [...]

    Last 30 days « Jalaj

    April 2, 2007 at 11:36 am


Leave a Reply