Jalaj

March 8, 2007

Asc() and Chr() in JavaScript

Filed under: JavaScript — Jalaj @ 10:39 am

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 :

5 Comments »

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

    Pingback by Last 30 days « Jalaj — April 2, 2007 @ 11:36 am

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

    Pingback by It’s 6 months since this blog started « Jalaj — June 12, 2007 @ 8:18 am

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

    Pingback by The Blog Revisited - 6 « Jalaj — October 31, 2007 @ 8:23 am

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

    Pingback by INT() and VAL() in JavaScript « Jalaj — November 29, 2007 @ 4:36 am

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

    Pingback by Year One Digest « Jalaj — December 12, 2007 @ 2:39 pm

RSS feed for comments on this post. TrackBack URI

Leave a comment

Blog at WordPress.com.