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 8th, 2007 at 10:39 am

Posted in JavaScript

Tagged with

6 Responses to 'Asc() and Chr() in JavaScript'

Subscribe to comments with RSS or TrackBack to 'Asc() and Chr() in JavaScript'.

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

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

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

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

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

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

    Zim

    7 May 09 at 7:41 pm

Leave a Reply