Hello
I try to learn the String.charCodeAt(index) method
Here is a snippet that demonstrate that the first index is allways 48 independentely of the character.
How work's it (I was expecting that each letter has her unicode-number equivalent.

function unicoding(str) {

  for (i = 0; i < str.length; i ++) {
    console.log(String.charCodeAt(i))
  }
  return str;
}

unicoding("abce");
unicoding("abcde");
unicoding("zabcdef");
unicoding("yzabcdefg");

48
49
50
51
48
49
50
51
52
48
49
50
51
52
53
54
48
49
50
51
52
53
54
55
56
=> 'yzabcdefg'   

what is the secret of this numbers.
Sorry if it is very trivial.

Recommended Answers

All 9 Replies

function unicoding(str) {
  for (i = 0; i < str.length; i ++) {
    console.log(String.charCodeAt(i))
  }
  return str;
}

You're not using the str variable but String, which should give you a type error.

2015-11-07--1446935403_476x330_scrot.png

Change it to str.charCodeAt(i).

My guess is that the engine you're using is interpreting String.charCodeAt(0) as String.charCodeAt('0') and giving you the unicode for 0, 1, 2, 3, 4 etc. which are the values of i given those str lengths you supplied. But, as you can see in the screenshot it won't compile on all JavaScript engines.

@Traevel
Fine. Thank you
I confused String.fromCodeAt() and ___.CharCodeAt()
The engine is repl.it
Is there a better engine.
repl.it is very easy but the storage of snippets is without name of file.

@rproffitt
What have you understand in this topic. It is hard for me.

@kouty. To me your question was why the 48 and such showed up. If we read the article then we see that number and learn more about unicode. The one thing I didn't see in your post is a clear question. You may be thinking in 8 bits?

@rproffit
you are extremely friendly !
ן will read it again. But my originaly question was derived from a stupid mistake between
String.fromCharCode() or __.charCodeAt()
Anyway thank you and all the best.

@kouty. Actually I found the discussion useful as I needed a refresher on Unicode. I hope you come back but one thing. Try to put the question mark on your question next time. I thought the question was why the 48 showed up, and not that you wanted some way from Unicode to say Ascii or other character. That is where I mussed up.

@rproffit
In fact, following the snippet of traevel, and following the unicode, 48 is the code of the digit "0".

0030: 48 is 0, 49 is 1, 50 is 2, 51 is 3 etc
I not know precisely but String gives "01234".charCode(<index 0 to 4 included>) and we get 48, 49 etc.
With a few quantity of stupidity I can discover a lot of things.

The engine is repl.it
Is there a better engine.

That one uses the same one as Firefox (SpiderMonkey), Node.js uses the one Chrome does (V8). You can even compare the two on that site I see. (For what it's worth, IE and Edge use the Chakra engines, Safari uses WebKit).

It's best to use code that compiles on all engines since your users will likely be on Firefox, Chrome, Safari or Edge as well. As you have noticed that isn't always as easy as it sounds.

commented: Web developers, second verse! +6
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.