3

When checking out the length property on a string I made a mistake and forgot the quotes around length. Instead of an error, one of the characters appeared from the string.

const string = 'name'
s[length]
>> "a"

I have done other combinations and usually get the second letter returned. So is the word length being converted into a Boolean, which is then converted to a number? Or, is something else going on?

Update. this is all I did:

const a = [1,2,3]
>> undefined
a["length"]
>> 3
const s = 'name'
>> undefined
s[length]
>> "a"
s['length']
>> 4
length
>> 1
a[length]
>> 2
9
  • 4
    Could you be using the variable length anywhere else in your code? Commented Mar 22, 2018 at 9:00
  • It was just in the console (Firefox), and I hadn't done much else. Commented Mar 22, 2018 at 9:01
  • Check it @SebastianSpeitel said because ("name")[undefined] = 'n' because it cast undefined to 0 Commented Mar 22, 2018 at 9:01
  • What does console.log(length) give you? Commented Mar 22, 2018 at 9:01
  • But OP said it returns the second char Commented Mar 22, 2018 at 9:02

3 Answers 3

4

In the default scope, length will evaluate to window.length, which is the number of frames or iframes in the window (usually 0, but in your case maybe 1?)

Edit: A quick look at the meaning of the different examples in your updated question.

const a = [1,2,3]; // Array of length 3
a["length"];       // equals a.length , 3
const s = 'name';  // String of length 4
s[length];         // s[window.length], s[1], 'a'
s['length'];       // equals s.length, 4
length;            // equals window.length, 1
a[length]          // equals a[window.length], a[1] , 2
Sign up to request clarification or add additional context in comments.

4 Comments

Good one!, Do you know other similar "shortcuts"?
@Naramsim, not entirely sure what you mean. In the 'default' or top level scope, this evaluates to 'window' which means that any property of 'window' can be accessed by it's short name, e.g. location points to window.location and so on
In the same manner, you can mostly use setInterval and don't have to use window.setInterval
@Naramsim: console.dir(window);
1

Not an answer, but a general piece of advice:

use s.length instead of s['length']

You avoid the mistake you just ran into.

I personally only use square brackets when I'm accessing values in a plain old array or when I need to use a variable or a property name that compilers don't like (ie. s[myVar] or s['some-property']).

3 Comments

Use the comment area for your valueable advice as this is the answer area.
He cant, I think, look at his reputation
Would if i could comment :)
0

Usually you do s.length as length is property of String.

If you have length variable somewhere in your code, then it's typecasted to integer.

If you do not have it, then undefined is converted to integer.


s[length] converts to s[0] in your case

1 Comment

That's completely wrong. Bracket notation casts to String, not to integer (and there is not even an integer type in js...).

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.