9

Is this code safe in all major browsers?

var string = '123'
alert(string[1] == '2') // should alert true
700 Software
  • 85,281
  • 83
  • 234
  • 341
  • 1
    jw. why do you want to do that, this is just as optimized. Not bashing... just curious. alert(string.indexOf("2") != -1); – Tim Joyce Apr 08 '11 at 19:31
  • @Tim: because that's not the same thing? Your condition checks if it appears in the string at all. He is checking the value of a specific position in the string. If you did string.indexOf("2") == 1) that would be the same thing. – CrayonViolent Apr 08 '11 at 19:34
  • @Tim When you do IndexOf('2') != -1. All this tells you is that '2' exists somewhere in the string. It does not verify that '2' is index 1. – clamchoda Apr 08 '11 at 19:52

5 Answers5

14

No, it's not safe. Internet Explorer 7 doesn't support accessing strings by index.

You have to use the charAt method to be compatibale with IE7:

var string = '123';
alert(string.charAt(1) == '2');
Guffa
  • 687,336
  • 108
  • 737
  • 1,005
3

Everything in JavaScript is an object; arrays, functions, strings, everything. The piece of code you put up is perfectly valid, although a little confusing - there are much better ways of doing that

var str = '123';
str[1] === '2'; // true, as you've just discovered (if you're not in IE7)
// Better ways:
str.indexOf('2'); // 1
str.charAt(1); // '2'
str.substr(1, 1); // '2'
str.split(''); // ['1', '2', '3']

The better ways make sure anyone else reading your code (either someone else or yourself in 6 months time) don't thing that str is an array. It makes your code a lot easier to read and maintain

James Long
  • 4,629
  • 1
  • 20
  • 30
  • 1
    Agreed. It's valid but it might make people think you are using an array instead of a string. Would not want to sacrifice that code readability esp when there are many other less ambiguous methods. – CrayonViolent Apr 08 '11 at 19:43
  • 1
    the indexOf method is NOT implemented for arrays in every browser ( cough cough IE cough cough ) – KeatsKelleher Apr 08 '11 at 19:51
  • 1
    `Array.prototype.indexOf` has limited but growing support. `String.prototype.indexOf` has been around for a while. – James Long Apr 08 '11 at 20:02
  • Accessing a string by index is not supported by all current browsers either. (IE7 sadly still has to be considered to be current...) – Guffa Apr 08 '11 at 20:09
  • I've updated my notes. I gave you +1 for spotting that, Guffa – James Long Apr 08 '11 at 20:13
  • Accessing characters as numeric properties of strings is non-standard as well as not universally supported. A string is not an array. – Tim Down Apr 08 '11 at 22:29
1

I tested in IE7, IE8, Safari, Chrome, and FF. All worked just fine!

EDIT just for kicks it works in Konqueror also! Js Fiddle example

Hacknightly
  • 5,109
  • 1
  • 26
  • 27
0

I don't really see why you can't do that...though alternatively, you can use .substring()

CrayonViolent
  • 32,111
  • 5
  • 56
  • 79
0

It will work. It might be a problem if you decide to use browser specific functions (I.E xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); only works in internet explorer)

alexyorke
  • 4,224
  • 3
  • 34
  • 55