The bracket notation for string is incomplete in Javascript and does not work in IE7. This is really painful to migrate to the .charAt(pos) equivalent, this is why i recommend you not to use it.

// Bracket Notation
"Hello World!"[6]
// > "W"
 
// Real Implementation
"Hello World!".charAt(6)
// > "W"

The bracket notation to get a character from a string is a shortcut to .charAt(pos) added by the vast majority of the browsers. However, i would not recommend to use it for several reasons.

This notation does not work in IE7. The first code snippet will return undefined in IE7. If you happen to use the bracket notation for strings all over your code and you want to migrate to .charAt(pos), this is a real pain: Brackets are used all over your code and there's no easy way to detect if that's for a string or an array/object.

You can't set the character using this notation. As there is no warning of any kind, this is really confusing and frustrating. If you were using the .charAt(pos) function, you would not have been tempted to do it.

var string = "Hello World!";
string[6] = '?';
console.log(string);
// > "Hello World!";

Random Posts

  • Project – CosmosUI (0 Comments) -- August 4, 2009

    CosmosUI is an open source interface modification of World of Warcraft. Many of the CosmosUI additions were later implemented by Blizzard on the default interface. I had been doing Warcraft III map making for more than a year when World of Warcraft has been leaked. This was really exciting to hac...

  • Project – WoR Guild Recruitment (0 Comments) -- December 23, 2009

    World of Raids Guild Recruitment Guild recruitment is a recurrent problem in World of Warcraft, many attempt have been made but none succedeed so far. After a brainstorming we decided that the following points were crucial. The guild recruiter has to spend less time as possible to set-up a...

  • Javascript – Full Dispatch (extended form of Multimethod) (0 Comments) -- January 12, 2010

    Existing Dispatch Methods Dispatch is the process of mapping a function call to a specific function based on its arguments. Most of the time this is done at runtime through the binding phase, however it usually lacks of modularity. Dispatch by Type The first way dispatch has been implemented is...

  • Javascript – Slug (0 Comments) -- February 20, 2010

    A slug is a way to represent a title with a limited charset (only lowercase letter and dash) to be inserted in the url. Even if it is a common function there is no good enough implentation when you Google for it. Here are the features I needed: No multiple dashes. ---- is converted to - No wr...

  • Javascript – Ajax Binary Reader (7 Comments) -- January 17, 2010

    With WebGL coming in, it is important to be able to deal with binary data files like the models. Since there is no such thing on the internet right now I decided to make my own. The javascript BinaryReader library tries to mimic the C# BinaryReader. The code is mostly based on the binary-parser c...

Trackback

No comments until now

Add your comment now