Bracket notation now works on all major browsers, except for IE7 and below.

// Bracket Notation
"Test String1"[6]

// charAt Implementation
"Test String1".charAt(6)

It used to be a bad idea to use brackets, for these reasons (Source):

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.

Answer from Brian Webster on Stack Overflow
🌐
DigitalOcean
digitalocean.com › community › tutorials › how-to-index-split-and-manipulate-strings-in-javascript
How To Index, Split, and Manipulate Strings in JavaScript | DigitalOcean
August 24, 2021 - Essentially, there are methods and properties available to all strings, and in the background JavaScript will perform a conversion to object and back to primitive every time a method or property is called. Each of the characters in a string correspond to an index number, starting with 0.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › String › indexOf
String.prototype.indexOf() - JavaScript | MDN
The method returns the index of the first occurrence of the specified substring at a position greater than or equal to position, which defaults to 0. If position is greater than the length of the calling string, the method doesn't search the calling string at all.
🌐
W3Schools
w3schools.com › jsref › jsref_indexof.asp
JavaScript String indexOf() Method
The indexOf() method returns the position of the first occurrence of a value in a string.
🌐
Gitbooks
buzzcoder.gitbooks.io › codecraft-javascript › content › string › string-indexing.html
String Indexing · CodeCraft - JavaScript
Characters are indexed from left to right, starting from the position 0, not 1! Blank spaces are also counted as characters. let str = "Hello"; console.log(str[0]); // H console.log(str[2]); // l · Note that strings are immutable, we can not change any character in a string, but we can create ...
Top answer
1 of 7
292

Bracket notation now works on all major browsers, except for IE7 and below.

// Bracket Notation
"Test String1"[6]

// charAt Implementation
"Test String1".charAt(6)

It used to be a bad idea to use brackets, for these reasons (Source):

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.

2 of 7
134

From MDN:

There are two ways to access an individual character in a string. The first is the charAt method, part of ECMAScript 3:

return 'cat'.charAt(1); // returns "a"

The other way is to treat the string as an array-like object, where each individual characters correspond to a numerical index. This has been supported by most browsers since their first version, except for IE. It was standardised in ECMAScript 5:

return 'cat'[1]; // returns "a"

The second way requires ECMAScript 5 support (and not supported in some older browsers).

In both cases, attempting to change an individual character won't work, as strings are immutable, i.e., their properties are neither neither "writable" nor "configurable".

  • str.charAt(i) is better from a compatibility perspective if IE6/IE7 compatibility is required.
  • str[i] is more modern and works in IE8+ and all other browsers (all Edge/Firefox/Chrome, Safari 2+, all iOS/Android).
🌐
Mimo
mimo.org › glossary › javascript › string-indexof-method
JavaScript String indexOf() method: Syntax, Usage, and Examples
The JavaScript string indexOf method is a powerful and efficient way to search inside strings and is commonly used in text processing, filtering, and validation.
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › how-to-find-the-index-of-specific-character-in-a-string-in-javascript
JavaScript - Index of a Character in String - GeeksforGeeks
July 23, 2025 - In this approach, we can iterate over each character in the string using a for loop and check if it matches the specific character we are looking for. If a match is found, we return the index of that character.
Find elsewhere
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › javascript-string-indexof-method
JavaScript String indexOf() Method - GeeksforGeeks
If the searchValue is found, the method returns the index of its first occurrence. If the searchValue is not found, the method returns -1. Here’s a simple example that finds the position of a substring within a string:
Published   December 27, 2017
🌐
JavaScript in Plain English
javascript.plainenglish.io › javascript-using-string-as-a-array-index-4392c21c98b3
JavaScript: Using String as a Array Index | by Wilson Filho | JavaScript in Plain English
August 9, 2024 - JavaScript: Using String as a Array Index Most people with practical purposes use a String as an array index when working with quickly implements in JavaScript/Node, but the behavior of the array …
🌐
JavaScript Tutorial
javascripttutorial.net › home › javascript string methods › string.prototype.indexof()
JavaScript String indexOf() Method
November 3, 2024 - Summary: in this tutorial, you’ll learn how to use the JavaScript String indexOf() method to find the index of the first occurrence of a substring within a string.
🌐
Vultr Docs
docs.vultr.com › javascript › standard-library › String › indexOf
JavaScript String indexOf() - Find Character Position | Vultr Docs
November 13, 2024 - The indexOf() method in JavaScript is a critical tool for finding the position of a substring within a string. This method returns the index of the first occurrence of the specified value, or -1 if the value isn't found.
🌐
30 Seconds of Code
30secondsofcode.org › home › javascript › string › all indexes of substring
Find all indexes of a substring in a JavaScript string - 30 seconds of code
March 10, 2024 - const indexOfSubstrings = function* (str, searchValue) { let i = 0; while (true) { const r = str.indexOf(searchValue, i); if (r !== -1) { yield r; i = r + 1; } else return; } }; [...indexOfSubstrings('tiktok tok tok tik tok tik', 'tik')]; // [0, 15, 23] [...indexOfSubstrings('tutut tut tut', 'tut')]; // [0, 2, 6, 10] [...indexOfSubstrings('hello', 'hi')]; // [] ... JavaScript generator functions are an advanced yet very powerful ES6 feature, which you can start using in your code right now. ... Get all the partial substrings of a string in JavaScript using generator functions.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › String › lastIndexOf
String.prototype.lastIndexOf() - JavaScript | MDN
The method returns the index of the last occurrence of the specified substring at a position less than or equal to position, which defaults to Infinity. If position is greater than the length of the calling string, the method searches the entire string.
🌐
Codedamn
codedamn.com › news › javascript
indexOf() String Method in JavaScript
June 6, 2023 - The indexOf() method is a built-in function in JavaScript that can be used to search for the first occurrence of a specified value within a string.
🌐
CloudSigma
blog.cloudsigma.com › home › customers › javascript: a tutorial on how to index, split, and manipulate strings
JavaScript: a Tutorial on How to Index, Split, and Manipulate Strings
February 7, 2023 - In this guide, we will show you how to differentiate string primitives and string objects in JavaScript. We will also discuss how you can index strings, access individual characters, and use some common properties and methods.