JavaScript property access: dot notation vs. brackets?










353















Other than the obvious fact that the first form could use a variable and not just a string literal, is there any reason to use one over the other, and if so under which cases?



In code:



// Given:
var foo = 'bar': 'baz';

// Then
var x = foo['bar'];

// vs.
var x = foo.bar;


Context: I've written a code generator which produces these expressions and I'm wondering which is preferable.










share|improve this question



















  • 2





    Just to chip in, not an answer to your original question (since you've had plenty of good explanations so far), but speed-wise there's no difference worth mentioning either: jsperf.com/dot-vs-square-brackets. The above test gives only a 2% margin at best to either of them, they're neck and neck.

    – unwitting
    Sep 4 '13 at 15:25











  • See also How do I add a property to an object using a variable as the name? and Dynamically access object property using variable

    – Bergi
    Nov 18 '14 at 6:12











  • This question/answer can be used also for UTF-8 keys.

    – Peter Krauss
    18 hours ago















353















Other than the obvious fact that the first form could use a variable and not just a string literal, is there any reason to use one over the other, and if so under which cases?



In code:



// Given:
var foo = 'bar': 'baz';

// Then
var x = foo['bar'];

// vs.
var x = foo.bar;


Context: I've written a code generator which produces these expressions and I'm wondering which is preferable.










share|improve this question



















  • 2





    Just to chip in, not an answer to your original question (since you've had plenty of good explanations so far), but speed-wise there's no difference worth mentioning either: jsperf.com/dot-vs-square-brackets. The above test gives only a 2% margin at best to either of them, they're neck and neck.

    – unwitting
    Sep 4 '13 at 15:25











  • See also How do I add a property to an object using a variable as the name? and Dynamically access object property using variable

    – Bergi
    Nov 18 '14 at 6:12











  • This question/answer can be used also for UTF-8 keys.

    – Peter Krauss
    18 hours ago













353












353








353


135






Other than the obvious fact that the first form could use a variable and not just a string literal, is there any reason to use one over the other, and if so under which cases?



In code:



// Given:
var foo = 'bar': 'baz';

// Then
var x = foo['bar'];

// vs.
var x = foo.bar;


Context: I've written a code generator which produces these expressions and I'm wondering which is preferable.










share|improve this question
















Other than the obvious fact that the first form could use a variable and not just a string literal, is there any reason to use one over the other, and if so under which cases?



In code:



// Given:
var foo = 'bar': 'baz';

// Then
var x = foo['bar'];

// vs.
var x = foo.bar;


Context: I've written a code generator which produces these expressions and I'm wondering which is preferable.







javascript syntax






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Aug 15 '17 at 16:57









Paul Roub

32.8k85875




32.8k85875










asked Feb 11 '11 at 11:21









Mark RenoufMark Renouf

21.9k1883116




21.9k1883116







  • 2





    Just to chip in, not an answer to your original question (since you've had plenty of good explanations so far), but speed-wise there's no difference worth mentioning either: jsperf.com/dot-vs-square-brackets. The above test gives only a 2% margin at best to either of them, they're neck and neck.

    – unwitting
    Sep 4 '13 at 15:25











  • See also How do I add a property to an object using a variable as the name? and Dynamically access object property using variable

    – Bergi
    Nov 18 '14 at 6:12











  • This question/answer can be used also for UTF-8 keys.

    – Peter Krauss
    18 hours ago












  • 2





    Just to chip in, not an answer to your original question (since you've had plenty of good explanations so far), but speed-wise there's no difference worth mentioning either: jsperf.com/dot-vs-square-brackets. The above test gives only a 2% margin at best to either of them, they're neck and neck.

    – unwitting
    Sep 4 '13 at 15:25











  • See also How do I add a property to an object using a variable as the name? and Dynamically access object property using variable

    – Bergi
    Nov 18 '14 at 6:12











  • This question/answer can be used also for UTF-8 keys.

    – Peter Krauss
    18 hours ago







2




2





Just to chip in, not an answer to your original question (since you've had plenty of good explanations so far), but speed-wise there's no difference worth mentioning either: jsperf.com/dot-vs-square-brackets. The above test gives only a 2% margin at best to either of them, they're neck and neck.

– unwitting
Sep 4 '13 at 15:25





Just to chip in, not an answer to your original question (since you've had plenty of good explanations so far), but speed-wise there's no difference worth mentioning either: jsperf.com/dot-vs-square-brackets. The above test gives only a 2% margin at best to either of them, they're neck and neck.

– unwitting
Sep 4 '13 at 15:25













See also How do I add a property to an object using a variable as the name? and Dynamically access object property using variable

– Bergi
Nov 18 '14 at 6:12





See also How do I add a property to an object using a variable as the name? and Dynamically access object property using variable

– Bergi
Nov 18 '14 at 6:12













This question/answer can be used also for UTF-8 keys.

– Peter Krauss
18 hours ago





This question/answer can be used also for UTF-8 keys.

– Peter Krauss
18 hours ago












11 Answers
11






active

oldest

votes


















382














(Sourced from here.)



Square bracket notation allows the use of characters that can't be used with dot notation:




var foo = myForm.foo; // incorrect syntax
var foo = myForm["foo"]; // correct syntax



including non-ASCII (UTF-8) characters, as in myForm["ダ"] (more examples).



Secondly, square bracket notation is useful when dealing with
property names which vary in a predictable way:




for (var i = 0; i < 10; i++) 
someFunction(myForm["myControlNumber" + i]);




Roundup:




  • Dot notation is faster to write and clearer to read.

  • Square bracket notation allows access to properties containing
    special characters and selection of
    properties using variables




Another example of characters that can't be used with dot notation is property names that themselves contain a dot.



For example a json response could contain a property called bar.Baz.



var foo = myResponse.bar.Baz; // incorrect syntax
var foo = myResponse["bar.Baz"]; // correct syntax





share|improve this answer




















  • 43





    The code examples and wording of the summary look awfully familiar. dev-archive.net/articles/js-dot-notation

    – Quentin
    Feb 11 '11 at 11:31






  • 56





    No need in re-inventing the wheel, is there? Citing it as a reference.

    – Aron Rotteveel
    Feb 11 '11 at 11:32







  • 4





    But I wonder, is one faster than the other?

    – BigOmega
    Mar 6 '13 at 11:24






  • 9





    Dot notation is faster (for me at least) test your browser jsperf.com/dot-notation-vs-bracket-notation/2

    – David Chen
    May 23 '13 at 16:55






  • 3





    in chrome 44 on my machine bracket notation is faster

    – Austin France
    Aug 12 '15 at 15:44


















92














The bracket notation allows you to access properties by name stored in a variable:



var obj = "abc" : "hello" ;
var x = "abc";
var y = obj[x];
console.log(y); //output - hello


obj.x would not work in this case.






share|improve this answer
































    8














    Dot notation does not work with some keywords (like new and class) in internet explorer 8.



    I had this code:



    //app.users is a hash
    app.users.new =
    // some code



    And this triggers the dreaded "expected indentifier" (at least on IE8 on windows xp, I havn't tried other environments). The simple fix for that is to switch to bracket notation:



    app.users['new'] = 
    // some code






    share|improve this answer























    • Helpful answer. Thank You.

      – Ilyas karim
      Apr 12 '18 at 9:57


















    8














    Be careful while using these notations:
    For eg. if we want to access a function present in the parent of a window.
    In IE :



    window['parent']['func']


    is not equivalent to



    window.['parent.func']


    We may either use:



    window['parent']['func'] 


    or



    window.parent.func 


    to access it






    share|improve this answer






























      7














      The two most common ways to access properties in JavaScript are with a dot and with square brackets. Both value.x and value[x] access a property on value—but not necessarily the same property. The difference is in how x is interpreted. When using a dot, the part after the dot must be a valid variable name, and it directly names the property. When using square brackets, the expression between the brackets is evaluated to get the property name. Whereas value.x fetches the property of value named “x”, value[x] tries to evaluate the expression x and uses the result as the property name.



      So if you know that the property you are interested in is called “length”, you say value.length. If you want to extract the property named by the value held in the variable i, you say value[i]. And because property names can be any string, if you want to access a property named “2” or “John Doe”, you must use square brackets: value[2] or value["John Doe"]. This is the case even though you know the precise name of the property in advance, because neither “2” nor “John Doe” is a valid variable name and so cannot be accessed through dot notation.



      In case of Arrays



      The elements in an array are stored in properties. Because the names of these properties are numbers and we often need to get their name from a variable, we have to use the bracket syntax to access them. The length property of an array tells us how many elements it contains. This property name is a valid variable name, and we know its name in advance, so to find the length of an array, you typically write array.length because that is easier to write than array["length"].






      share|improve this answer























      • Could you elaborate more on array.length? You say that properties accessed by dot notation are not evaluated so in case of array.length wouldn't it give us "length" string instead of evaluated value, in this case the number of items in array? The elements in an array are stored in properties this is what confuses me. What do you mean by stored in properties? What are properties? In my understanding array is just bunch of values without properties. If it they are stored in properties, how come it is not property: value/associative array?

        – Limpuls
        Nov 19 '17 at 15:04











      • This answer is particularly valuable because it explains the difference between the two notations.

        – chessweb
        Nov 1 '18 at 1:39


















      6














      Generally speaking, they do the same job.

      Nevertheless, the bracket notation gives you the opportunity to do stuff that you can't do with dot notation, like



      var x = elem["foo"]; // can't do elem.foo;


      This can be extended to any property containing special characters.






      share|improve this answer






























        5














        You need to use brackets if the property names has special characters:



        var foo = 
        "Hello, world!": true,

        foo["Hello, world!"] = false;


        Other than that, I suppose it's just a matter of taste. IMHO, the dot notation is shorter and it makes it more obvious that it's a property rather than an array element (although of course JavaScript does not have associative arrays anyway).






        share|improve this answer
































          3














          Bracket notation can use variables, so it is useful in two instances where dot notation will not work:



          1) When the property names are dynamically determined (when the exact names are not known until runtime).



          2) When using a for..in loop to go through all the properties of an object.



          source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Working_with_Objects






          share|improve this answer






























            2














            You have to use square bracket notation when -




            1. The property name is number.



              var ob = 
              1: 'One',
              7 : 'Seven'

              ob.7 // SyntaxError
              ob[7] // "Seven"



            2. The property name has special character.



              var ob = 
              'This is one': 1,
              'This is seven': 7,

              ob.'This is one' // SyntaxError
              ob['This is one'] // 1



            3. The property name is assigned to a variable and you want to access the
              property value by this variable.



              var ob = 
              'One': 1,
              'Seven': 7,


              var _Seven = 'Seven';
              ob._Seven // undefined
              ob[_Seven] // 7






            share|improve this answer
































              0














              Case where notation is helpful :



              If your object is dynamic and there could be some random values in keys like number and or any other special character, for example -



              var a = 1 : 3 ;



              Now if you try to access in like a.1 it will through an error, because it is expecting an string over there.






              share|improve this answer






























                0














                Let me add some more use case of the square-bracket notation. If you want to access a property say x-proxy in a object, then - will be interpreted wrongly. Their are some other cases too like space, dot, etc., where dot operation will not help you. Also if u have the key in a variable then only way to access the value of the key in a object is by bracket notation. Hope you get some more context.






                share|improve this answer





















                  protected by Samuel Liew Oct 5 '15 at 9:02



                  Thank you for your interest in this question.
                  Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).



                  Would you like to answer one of these unanswered questions instead?














                  11 Answers
                  11






                  active

                  oldest

                  votes








                  11 Answers
                  11






                  active

                  oldest

                  votes









                  active

                  oldest

                  votes






                  active

                  oldest

                  votes









                  382














                  (Sourced from here.)



                  Square bracket notation allows the use of characters that can't be used with dot notation:




                  var foo = myForm.foo; // incorrect syntax
                  var foo = myForm["foo"]; // correct syntax



                  including non-ASCII (UTF-8) characters, as in myForm["ダ"] (more examples).



                  Secondly, square bracket notation is useful when dealing with
                  property names which vary in a predictable way:




                  for (var i = 0; i < 10; i++) 
                  someFunction(myForm["myControlNumber" + i]);




                  Roundup:




                  • Dot notation is faster to write and clearer to read.

                  • Square bracket notation allows access to properties containing
                    special characters and selection of
                    properties using variables




                  Another example of characters that can't be used with dot notation is property names that themselves contain a dot.



                  For example a json response could contain a property called bar.Baz.



                  var foo = myResponse.bar.Baz; // incorrect syntax
                  var foo = myResponse["bar.Baz"]; // correct syntax





                  share|improve this answer




















                  • 43





                    The code examples and wording of the summary look awfully familiar. dev-archive.net/articles/js-dot-notation

                    – Quentin
                    Feb 11 '11 at 11:31






                  • 56





                    No need in re-inventing the wheel, is there? Citing it as a reference.

                    – Aron Rotteveel
                    Feb 11 '11 at 11:32







                  • 4





                    But I wonder, is one faster than the other?

                    – BigOmega
                    Mar 6 '13 at 11:24






                  • 9





                    Dot notation is faster (for me at least) test your browser jsperf.com/dot-notation-vs-bracket-notation/2

                    – David Chen
                    May 23 '13 at 16:55






                  • 3





                    in chrome 44 on my machine bracket notation is faster

                    – Austin France
                    Aug 12 '15 at 15:44















                  382














                  (Sourced from here.)



                  Square bracket notation allows the use of characters that can't be used with dot notation:




                  var foo = myForm.foo; // incorrect syntax
                  var foo = myForm["foo"]; // correct syntax



                  including non-ASCII (UTF-8) characters, as in myForm["ダ"] (more examples).



                  Secondly, square bracket notation is useful when dealing with
                  property names which vary in a predictable way:




                  for (var i = 0; i < 10; i++) 
                  someFunction(myForm["myControlNumber" + i]);




                  Roundup:




                  • Dot notation is faster to write and clearer to read.

                  • Square bracket notation allows access to properties containing
                    special characters and selection of
                    properties using variables




                  Another example of characters that can't be used with dot notation is property names that themselves contain a dot.



                  For example a json response could contain a property called bar.Baz.



                  var foo = myResponse.bar.Baz; // incorrect syntax
                  var foo = myResponse["bar.Baz"]; // correct syntax





                  share|improve this answer




















                  • 43





                    The code examples and wording of the summary look awfully familiar. dev-archive.net/articles/js-dot-notation

                    – Quentin
                    Feb 11 '11 at 11:31






                  • 56





                    No need in re-inventing the wheel, is there? Citing it as a reference.

                    – Aron Rotteveel
                    Feb 11 '11 at 11:32







                  • 4





                    But I wonder, is one faster than the other?

                    – BigOmega
                    Mar 6 '13 at 11:24






                  • 9





                    Dot notation is faster (for me at least) test your browser jsperf.com/dot-notation-vs-bracket-notation/2

                    – David Chen
                    May 23 '13 at 16:55






                  • 3





                    in chrome 44 on my machine bracket notation is faster

                    – Austin France
                    Aug 12 '15 at 15:44













                  382












                  382








                  382







                  (Sourced from here.)



                  Square bracket notation allows the use of characters that can't be used with dot notation:




                  var foo = myForm.foo; // incorrect syntax
                  var foo = myForm["foo"]; // correct syntax



                  including non-ASCII (UTF-8) characters, as in myForm["ダ"] (more examples).



                  Secondly, square bracket notation is useful when dealing with
                  property names which vary in a predictable way:




                  for (var i = 0; i < 10; i++) 
                  someFunction(myForm["myControlNumber" + i]);




                  Roundup:




                  • Dot notation is faster to write and clearer to read.

                  • Square bracket notation allows access to properties containing
                    special characters and selection of
                    properties using variables




                  Another example of characters that can't be used with dot notation is property names that themselves contain a dot.



                  For example a json response could contain a property called bar.Baz.



                  var foo = myResponse.bar.Baz; // incorrect syntax
                  var foo = myResponse["bar.Baz"]; // correct syntax





                  share|improve this answer















                  (Sourced from here.)



                  Square bracket notation allows the use of characters that can't be used with dot notation:




                  var foo = myForm.foo; // incorrect syntax
                  var foo = myForm["foo"]; // correct syntax



                  including non-ASCII (UTF-8) characters, as in myForm["ダ"] (more examples).



                  Secondly, square bracket notation is useful when dealing with
                  property names which vary in a predictable way:




                  for (var i = 0; i < 10; i++) 
                  someFunction(myForm["myControlNumber" + i]);




                  Roundup:




                  • Dot notation is faster to write and clearer to read.

                  • Square bracket notation allows access to properties containing
                    special characters and selection of
                    properties using variables




                  Another example of characters that can't be used with dot notation is property names that themselves contain a dot.



                  For example a json response could contain a property called bar.Baz.



                  var foo = myResponse.bar.Baz; // incorrect syntax
                  var foo = myResponse["bar.Baz"]; // correct syntax






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited 18 hours ago









                  Peter Krauss

                  5,4461084174




                  5,4461084174










                  answered Feb 11 '11 at 11:26









                  Aron RotteveelAron Rotteveel

                  52.1k1790124




                  52.1k1790124







                  • 43





                    The code examples and wording of the summary look awfully familiar. dev-archive.net/articles/js-dot-notation

                    – Quentin
                    Feb 11 '11 at 11:31






                  • 56





                    No need in re-inventing the wheel, is there? Citing it as a reference.

                    – Aron Rotteveel
                    Feb 11 '11 at 11:32







                  • 4





                    But I wonder, is one faster than the other?

                    – BigOmega
                    Mar 6 '13 at 11:24






                  • 9





                    Dot notation is faster (for me at least) test your browser jsperf.com/dot-notation-vs-bracket-notation/2

                    – David Chen
                    May 23 '13 at 16:55






                  • 3





                    in chrome 44 on my machine bracket notation is faster

                    – Austin France
                    Aug 12 '15 at 15:44












                  • 43





                    The code examples and wording of the summary look awfully familiar. dev-archive.net/articles/js-dot-notation

                    – Quentin
                    Feb 11 '11 at 11:31






                  • 56





                    No need in re-inventing the wheel, is there? Citing it as a reference.

                    – Aron Rotteveel
                    Feb 11 '11 at 11:32







                  • 4





                    But I wonder, is one faster than the other?

                    – BigOmega
                    Mar 6 '13 at 11:24






                  • 9





                    Dot notation is faster (for me at least) test your browser jsperf.com/dot-notation-vs-bracket-notation/2

                    – David Chen
                    May 23 '13 at 16:55






                  • 3





                    in chrome 44 on my machine bracket notation is faster

                    – Austin France
                    Aug 12 '15 at 15:44







                  43




                  43





                  The code examples and wording of the summary look awfully familiar. dev-archive.net/articles/js-dot-notation

                  – Quentin
                  Feb 11 '11 at 11:31





                  The code examples and wording of the summary look awfully familiar. dev-archive.net/articles/js-dot-notation

                  – Quentin
                  Feb 11 '11 at 11:31




                  56




                  56





                  No need in re-inventing the wheel, is there? Citing it as a reference.

                  – Aron Rotteveel
                  Feb 11 '11 at 11:32






                  No need in re-inventing the wheel, is there? Citing it as a reference.

                  – Aron Rotteveel
                  Feb 11 '11 at 11:32





                  4




                  4





                  But I wonder, is one faster than the other?

                  – BigOmega
                  Mar 6 '13 at 11:24





                  But I wonder, is one faster than the other?

                  – BigOmega
                  Mar 6 '13 at 11:24




                  9




                  9





                  Dot notation is faster (for me at least) test your browser jsperf.com/dot-notation-vs-bracket-notation/2

                  – David Chen
                  May 23 '13 at 16:55





                  Dot notation is faster (for me at least) test your browser jsperf.com/dot-notation-vs-bracket-notation/2

                  – David Chen
                  May 23 '13 at 16:55




                  3




                  3





                  in chrome 44 on my machine bracket notation is faster

                  – Austin France
                  Aug 12 '15 at 15:44





                  in chrome 44 on my machine bracket notation is faster

                  – Austin France
                  Aug 12 '15 at 15:44













                  92














                  The bracket notation allows you to access properties by name stored in a variable:



                  var obj = "abc" : "hello" ;
                  var x = "abc";
                  var y = obj[x];
                  console.log(y); //output - hello


                  obj.x would not work in this case.






                  share|improve this answer





























                    92














                    The bracket notation allows you to access properties by name stored in a variable:



                    var obj = "abc" : "hello" ;
                    var x = "abc";
                    var y = obj[x];
                    console.log(y); //output - hello


                    obj.x would not work in this case.






                    share|improve this answer



























                      92












                      92








                      92







                      The bracket notation allows you to access properties by name stored in a variable:



                      var obj = "abc" : "hello" ;
                      var x = "abc";
                      var y = obj[x];
                      console.log(y); //output - hello


                      obj.x would not work in this case.






                      share|improve this answer















                      The bracket notation allows you to access properties by name stored in a variable:



                      var obj = "abc" : "hello" ;
                      var x = "abc";
                      var y = obj[x];
                      console.log(y); //output - hello


                      obj.x would not work in this case.







                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited Sep 24 '13 at 14:54









                      Aaron Digulla

                      248k85475695




                      248k85475695










                      answered Feb 11 '11 at 11:27









                      naiquevinnaiquevin

                      4,44364061




                      4,44364061





















                          8














                          Dot notation does not work with some keywords (like new and class) in internet explorer 8.



                          I had this code:



                          //app.users is a hash
                          app.users.new =
                          // some code



                          And this triggers the dreaded "expected indentifier" (at least on IE8 on windows xp, I havn't tried other environments). The simple fix for that is to switch to bracket notation:



                          app.users['new'] = 
                          // some code






                          share|improve this answer























                          • Helpful answer. Thank You.

                            – Ilyas karim
                            Apr 12 '18 at 9:57















                          8














                          Dot notation does not work with some keywords (like new and class) in internet explorer 8.



                          I had this code:



                          //app.users is a hash
                          app.users.new =
                          // some code



                          And this triggers the dreaded "expected indentifier" (at least on IE8 on windows xp, I havn't tried other environments). The simple fix for that is to switch to bracket notation:



                          app.users['new'] = 
                          // some code






                          share|improve this answer























                          • Helpful answer. Thank You.

                            – Ilyas karim
                            Apr 12 '18 at 9:57













                          8












                          8








                          8







                          Dot notation does not work with some keywords (like new and class) in internet explorer 8.



                          I had this code:



                          //app.users is a hash
                          app.users.new =
                          // some code



                          And this triggers the dreaded "expected indentifier" (at least on IE8 on windows xp, I havn't tried other environments). The simple fix for that is to switch to bracket notation:



                          app.users['new'] = 
                          // some code






                          share|improve this answer













                          Dot notation does not work with some keywords (like new and class) in internet explorer 8.



                          I had this code:



                          //app.users is a hash
                          app.users.new =
                          // some code



                          And this triggers the dreaded "expected indentifier" (at least on IE8 on windows xp, I havn't tried other environments). The simple fix for that is to switch to bracket notation:



                          app.users['new'] = 
                          // some code







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Oct 4 '13 at 9:26









                          Benjamin CrouzierBenjamin Crouzier

                          23.5k30122186




                          23.5k30122186












                          • Helpful answer. Thank You.

                            – Ilyas karim
                            Apr 12 '18 at 9:57

















                          • Helpful answer. Thank You.

                            – Ilyas karim
                            Apr 12 '18 at 9:57
















                          Helpful answer. Thank You.

                          – Ilyas karim
                          Apr 12 '18 at 9:57





                          Helpful answer. Thank You.

                          – Ilyas karim
                          Apr 12 '18 at 9:57











                          8














                          Be careful while using these notations:
                          For eg. if we want to access a function present in the parent of a window.
                          In IE :



                          window['parent']['func']


                          is not equivalent to



                          window.['parent.func']


                          We may either use:



                          window['parent']['func'] 


                          or



                          window.parent.func 


                          to access it






                          share|improve this answer



























                            8














                            Be careful while using these notations:
                            For eg. if we want to access a function present in the parent of a window.
                            In IE :



                            window['parent']['func']


                            is not equivalent to



                            window.['parent.func']


                            We may either use:



                            window['parent']['func'] 


                            or



                            window.parent.func 


                            to access it






                            share|improve this answer

























                              8












                              8








                              8







                              Be careful while using these notations:
                              For eg. if we want to access a function present in the parent of a window.
                              In IE :



                              window['parent']['func']


                              is not equivalent to



                              window.['parent.func']


                              We may either use:



                              window['parent']['func'] 


                              or



                              window.parent.func 


                              to access it






                              share|improve this answer













                              Be careful while using these notations:
                              For eg. if we want to access a function present in the parent of a window.
                              In IE :



                              window['parent']['func']


                              is not equivalent to



                              window.['parent.func']


                              We may either use:



                              window['parent']['func'] 


                              or



                              window.parent.func 


                              to access it







                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered Apr 25 '14 at 7:11









                              user2593104user2593104

                              12311




                              12311





















                                  7














                                  The two most common ways to access properties in JavaScript are with a dot and with square brackets. Both value.x and value[x] access a property on value—but not necessarily the same property. The difference is in how x is interpreted. When using a dot, the part after the dot must be a valid variable name, and it directly names the property. When using square brackets, the expression between the brackets is evaluated to get the property name. Whereas value.x fetches the property of value named “x”, value[x] tries to evaluate the expression x and uses the result as the property name.



                                  So if you know that the property you are interested in is called “length”, you say value.length. If you want to extract the property named by the value held in the variable i, you say value[i]. And because property names can be any string, if you want to access a property named “2” or “John Doe”, you must use square brackets: value[2] or value["John Doe"]. This is the case even though you know the precise name of the property in advance, because neither “2” nor “John Doe” is a valid variable name and so cannot be accessed through dot notation.



                                  In case of Arrays



                                  The elements in an array are stored in properties. Because the names of these properties are numbers and we often need to get their name from a variable, we have to use the bracket syntax to access them. The length property of an array tells us how many elements it contains. This property name is a valid variable name, and we know its name in advance, so to find the length of an array, you typically write array.length because that is easier to write than array["length"].






                                  share|improve this answer























                                  • Could you elaborate more on array.length? You say that properties accessed by dot notation are not evaluated so in case of array.length wouldn't it give us "length" string instead of evaluated value, in this case the number of items in array? The elements in an array are stored in properties this is what confuses me. What do you mean by stored in properties? What are properties? In my understanding array is just bunch of values without properties. If it they are stored in properties, how come it is not property: value/associative array?

                                    – Limpuls
                                    Nov 19 '17 at 15:04











                                  • This answer is particularly valuable because it explains the difference between the two notations.

                                    – chessweb
                                    Nov 1 '18 at 1:39















                                  7














                                  The two most common ways to access properties in JavaScript are with a dot and with square brackets. Both value.x and value[x] access a property on value—but not necessarily the same property. The difference is in how x is interpreted. When using a dot, the part after the dot must be a valid variable name, and it directly names the property. When using square brackets, the expression between the brackets is evaluated to get the property name. Whereas value.x fetches the property of value named “x”, value[x] tries to evaluate the expression x and uses the result as the property name.



                                  So if you know that the property you are interested in is called “length”, you say value.length. If you want to extract the property named by the value held in the variable i, you say value[i]. And because property names can be any string, if you want to access a property named “2” or “John Doe”, you must use square brackets: value[2] or value["John Doe"]. This is the case even though you know the precise name of the property in advance, because neither “2” nor “John Doe” is a valid variable name and so cannot be accessed through dot notation.



                                  In case of Arrays



                                  The elements in an array are stored in properties. Because the names of these properties are numbers and we often need to get their name from a variable, we have to use the bracket syntax to access them. The length property of an array tells us how many elements it contains. This property name is a valid variable name, and we know its name in advance, so to find the length of an array, you typically write array.length because that is easier to write than array["length"].






                                  share|improve this answer























                                  • Could you elaborate more on array.length? You say that properties accessed by dot notation are not evaluated so in case of array.length wouldn't it give us "length" string instead of evaluated value, in this case the number of items in array? The elements in an array are stored in properties this is what confuses me. What do you mean by stored in properties? What are properties? In my understanding array is just bunch of values without properties. If it they are stored in properties, how come it is not property: value/associative array?

                                    – Limpuls
                                    Nov 19 '17 at 15:04











                                  • This answer is particularly valuable because it explains the difference between the two notations.

                                    – chessweb
                                    Nov 1 '18 at 1:39













                                  7












                                  7








                                  7







                                  The two most common ways to access properties in JavaScript are with a dot and with square brackets. Both value.x and value[x] access a property on value—but not necessarily the same property. The difference is in how x is interpreted. When using a dot, the part after the dot must be a valid variable name, and it directly names the property. When using square brackets, the expression between the brackets is evaluated to get the property name. Whereas value.x fetches the property of value named “x”, value[x] tries to evaluate the expression x and uses the result as the property name.



                                  So if you know that the property you are interested in is called “length”, you say value.length. If you want to extract the property named by the value held in the variable i, you say value[i]. And because property names can be any string, if you want to access a property named “2” or “John Doe”, you must use square brackets: value[2] or value["John Doe"]. This is the case even though you know the precise name of the property in advance, because neither “2” nor “John Doe” is a valid variable name and so cannot be accessed through dot notation.



                                  In case of Arrays



                                  The elements in an array are stored in properties. Because the names of these properties are numbers and we often need to get their name from a variable, we have to use the bracket syntax to access them. The length property of an array tells us how many elements it contains. This property name is a valid variable name, and we know its name in advance, so to find the length of an array, you typically write array.length because that is easier to write than array["length"].






                                  share|improve this answer













                                  The two most common ways to access properties in JavaScript are with a dot and with square brackets. Both value.x and value[x] access a property on value—but not necessarily the same property. The difference is in how x is interpreted. When using a dot, the part after the dot must be a valid variable name, and it directly names the property. When using square brackets, the expression between the brackets is evaluated to get the property name. Whereas value.x fetches the property of value named “x”, value[x] tries to evaluate the expression x and uses the result as the property name.



                                  So if you know that the property you are interested in is called “length”, you say value.length. If you want to extract the property named by the value held in the variable i, you say value[i]. And because property names can be any string, if you want to access a property named “2” or “John Doe”, you must use square brackets: value[2] or value["John Doe"]. This is the case even though you know the precise name of the property in advance, because neither “2” nor “John Doe” is a valid variable name and so cannot be accessed through dot notation.



                                  In case of Arrays



                                  The elements in an array are stored in properties. Because the names of these properties are numbers and we often need to get their name from a variable, we have to use the bracket syntax to access them. The length property of an array tells us how many elements it contains. This property name is a valid variable name, and we know its name in advance, so to find the length of an array, you typically write array.length because that is easier to write than array["length"].







                                  share|improve this answer












                                  share|improve this answer



                                  share|improve this answer










                                  answered Jul 18 '17 at 17:47









                                  Sagar MunjalSagar Munjal

                                  40359




                                  40359












                                  • Could you elaborate more on array.length? You say that properties accessed by dot notation are not evaluated so in case of array.length wouldn't it give us "length" string instead of evaluated value, in this case the number of items in array? The elements in an array are stored in properties this is what confuses me. What do you mean by stored in properties? What are properties? In my understanding array is just bunch of values without properties. If it they are stored in properties, how come it is not property: value/associative array?

                                    – Limpuls
                                    Nov 19 '17 at 15:04











                                  • This answer is particularly valuable because it explains the difference between the two notations.

                                    – chessweb
                                    Nov 1 '18 at 1:39

















                                  • Could you elaborate more on array.length? You say that properties accessed by dot notation are not evaluated so in case of array.length wouldn't it give us "length" string instead of evaluated value, in this case the number of items in array? The elements in an array are stored in properties this is what confuses me. What do you mean by stored in properties? What are properties? In my understanding array is just bunch of values without properties. If it they are stored in properties, how come it is not property: value/associative array?

                                    – Limpuls
                                    Nov 19 '17 at 15:04











                                  • This answer is particularly valuable because it explains the difference between the two notations.

                                    – chessweb
                                    Nov 1 '18 at 1:39
















                                  Could you elaborate more on array.length? You say that properties accessed by dot notation are not evaluated so in case of array.length wouldn't it give us "length" string instead of evaluated value, in this case the number of items in array? The elements in an array are stored in properties this is what confuses me. What do you mean by stored in properties? What are properties? In my understanding array is just bunch of values without properties. If it they are stored in properties, how come it is not property: value/associative array?

                                  – Limpuls
                                  Nov 19 '17 at 15:04





                                  Could you elaborate more on array.length? You say that properties accessed by dot notation are not evaluated so in case of array.length wouldn't it give us "length" string instead of evaluated value, in this case the number of items in array? The elements in an array are stored in properties this is what confuses me. What do you mean by stored in properties? What are properties? In my understanding array is just bunch of values without properties. If it they are stored in properties, how come it is not property: value/associative array?

                                  – Limpuls
                                  Nov 19 '17 at 15:04













                                  This answer is particularly valuable because it explains the difference between the two notations.

                                  – chessweb
                                  Nov 1 '18 at 1:39





                                  This answer is particularly valuable because it explains the difference between the two notations.

                                  – chessweb
                                  Nov 1 '18 at 1:39











                                  6














                                  Generally speaking, they do the same job.

                                  Nevertheless, the bracket notation gives you the opportunity to do stuff that you can't do with dot notation, like



                                  var x = elem["foo"]; // can't do elem.foo;


                                  This can be extended to any property containing special characters.






                                  share|improve this answer



























                                    6














                                    Generally speaking, they do the same job.

                                    Nevertheless, the bracket notation gives you the opportunity to do stuff that you can't do with dot notation, like



                                    var x = elem["foo"]; // can't do elem.foo;


                                    This can be extended to any property containing special characters.






                                    share|improve this answer

























                                      6












                                      6








                                      6







                                      Generally speaking, they do the same job.

                                      Nevertheless, the bracket notation gives you the opportunity to do stuff that you can't do with dot notation, like



                                      var x = elem["foo"]; // can't do elem.foo;


                                      This can be extended to any property containing special characters.






                                      share|improve this answer













                                      Generally speaking, they do the same job.

                                      Nevertheless, the bracket notation gives you the opportunity to do stuff that you can't do with dot notation, like



                                      var x = elem["foo"]; // can't do elem.foo;


                                      This can be extended to any property containing special characters.







                                      share|improve this answer












                                      share|improve this answer



                                      share|improve this answer










                                      answered Feb 11 '11 at 11:32









                                      CdBCdB

                                      2,94743661




                                      2,94743661





















                                          5














                                          You need to use brackets if the property names has special characters:



                                          var foo = 
                                          "Hello, world!": true,

                                          foo["Hello, world!"] = false;


                                          Other than that, I suppose it's just a matter of taste. IMHO, the dot notation is shorter and it makes it more obvious that it's a property rather than an array element (although of course JavaScript does not have associative arrays anyway).






                                          share|improve this answer





























                                            5














                                            You need to use brackets if the property names has special characters:



                                            var foo = 
                                            "Hello, world!": true,

                                            foo["Hello, world!"] = false;


                                            Other than that, I suppose it's just a matter of taste. IMHO, the dot notation is shorter and it makes it more obvious that it's a property rather than an array element (although of course JavaScript does not have associative arrays anyway).






                                            share|improve this answer



























                                              5












                                              5








                                              5







                                              You need to use brackets if the property names has special characters:



                                              var foo = 
                                              "Hello, world!": true,

                                              foo["Hello, world!"] = false;


                                              Other than that, I suppose it's just a matter of taste. IMHO, the dot notation is shorter and it makes it more obvious that it's a property rather than an array element (although of course JavaScript does not have associative arrays anyway).






                                              share|improve this answer















                                              You need to use brackets if the property names has special characters:



                                              var foo = 
                                              "Hello, world!": true,

                                              foo["Hello, world!"] = false;


                                              Other than that, I suppose it's just a matter of taste. IMHO, the dot notation is shorter and it makes it more obvious that it's a property rather than an array element (although of course JavaScript does not have associative arrays anyway).







                                              share|improve this answer














                                              share|improve this answer



                                              share|improve this answer








                                              edited Feb 29 '16 at 17:19

























                                              answered Feb 11 '11 at 11:25









                                              Álvaro GonzálezÁlvaro González

                                              106k30187277




                                              106k30187277





















                                                  3














                                                  Bracket notation can use variables, so it is useful in two instances where dot notation will not work:



                                                  1) When the property names are dynamically determined (when the exact names are not known until runtime).



                                                  2) When using a for..in loop to go through all the properties of an object.



                                                  source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Working_with_Objects






                                                  share|improve this answer



























                                                    3














                                                    Bracket notation can use variables, so it is useful in two instances where dot notation will not work:



                                                    1) When the property names are dynamically determined (when the exact names are not known until runtime).



                                                    2) When using a for..in loop to go through all the properties of an object.



                                                    source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Working_with_Objects






                                                    share|improve this answer

























                                                      3












                                                      3








                                                      3







                                                      Bracket notation can use variables, so it is useful in two instances where dot notation will not work:



                                                      1) When the property names are dynamically determined (when the exact names are not known until runtime).



                                                      2) When using a for..in loop to go through all the properties of an object.



                                                      source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Working_with_Objects






                                                      share|improve this answer













                                                      Bracket notation can use variables, so it is useful in two instances where dot notation will not work:



                                                      1) When the property names are dynamically determined (when the exact names are not known until runtime).



                                                      2) When using a for..in loop to go through all the properties of an object.



                                                      source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Working_with_Objects







                                                      share|improve this answer












                                                      share|improve this answer



                                                      share|improve this answer










                                                      answered Feb 19 '14 at 1:52









                                                      Lev StefanovichLev Stefanovich

                                                      1205




                                                      1205





















                                                          2














                                                          You have to use square bracket notation when -




                                                          1. The property name is number.



                                                            var ob = 
                                                            1: 'One',
                                                            7 : 'Seven'

                                                            ob.7 // SyntaxError
                                                            ob[7] // "Seven"



                                                          2. The property name has special character.



                                                            var ob = 
                                                            'This is one': 1,
                                                            'This is seven': 7,

                                                            ob.'This is one' // SyntaxError
                                                            ob['This is one'] // 1



                                                          3. The property name is assigned to a variable and you want to access the
                                                            property value by this variable.



                                                            var ob = 
                                                            'One': 1,
                                                            'Seven': 7,


                                                            var _Seven = 'Seven';
                                                            ob._Seven // undefined
                                                            ob[_Seven] // 7






                                                          share|improve this answer





























                                                            2














                                                            You have to use square bracket notation when -




                                                            1. The property name is number.



                                                              var ob = 
                                                              1: 'One',
                                                              7 : 'Seven'

                                                              ob.7 // SyntaxError
                                                              ob[7] // "Seven"



                                                            2. The property name has special character.



                                                              var ob = 
                                                              'This is one': 1,
                                                              'This is seven': 7,

                                                              ob.'This is one' // SyntaxError
                                                              ob['This is one'] // 1



                                                            3. The property name is assigned to a variable and you want to access the
                                                              property value by this variable.



                                                              var ob = 
                                                              'One': 1,
                                                              'Seven': 7,


                                                              var _Seven = 'Seven';
                                                              ob._Seven // undefined
                                                              ob[_Seven] // 7






                                                            share|improve this answer



























                                                              2












                                                              2








                                                              2







                                                              You have to use square bracket notation when -




                                                              1. The property name is number.



                                                                var ob = 
                                                                1: 'One',
                                                                7 : 'Seven'

                                                                ob.7 // SyntaxError
                                                                ob[7] // "Seven"



                                                              2. The property name has special character.



                                                                var ob = 
                                                                'This is one': 1,
                                                                'This is seven': 7,

                                                                ob.'This is one' // SyntaxError
                                                                ob['This is one'] // 1



                                                              3. The property name is assigned to a variable and you want to access the
                                                                property value by this variable.



                                                                var ob = 
                                                                'One': 1,
                                                                'Seven': 7,


                                                                var _Seven = 'Seven';
                                                                ob._Seven // undefined
                                                                ob[_Seven] // 7






                                                              share|improve this answer















                                                              You have to use square bracket notation when -




                                                              1. The property name is number.



                                                                var ob = 
                                                                1: 'One',
                                                                7 : 'Seven'

                                                                ob.7 // SyntaxError
                                                                ob[7] // "Seven"



                                                              2. The property name has special character.



                                                                var ob = 
                                                                'This is one': 1,
                                                                'This is seven': 7,

                                                                ob.'This is one' // SyntaxError
                                                                ob['This is one'] // 1



                                                              3. The property name is assigned to a variable and you want to access the
                                                                property value by this variable.



                                                                var ob = 
                                                                'One': 1,
                                                                'Seven': 7,


                                                                var _Seven = 'Seven';
                                                                ob._Seven // undefined
                                                                ob[_Seven] // 7







                                                              share|improve this answer














                                                              share|improve this answer



                                                              share|improve this answer








                                                              edited Jan 3 at 21:38









                                                              Jason Roman

                                                              6,314102530




                                                              6,314102530










                                                              answered Jun 7 '18 at 9:59









                                                              Harunur RashidHarunur Rashid

                                                              1,288611




                                                              1,288611





















                                                                  0














                                                                  Case where notation is helpful :



                                                                  If your object is dynamic and there could be some random values in keys like number and or any other special character, for example -



                                                                  var a = 1 : 3 ;



                                                                  Now if you try to access in like a.1 it will through an error, because it is expecting an string over there.






                                                                  share|improve this answer



























                                                                    0














                                                                    Case where notation is helpful :



                                                                    If your object is dynamic and there could be some random values in keys like number and or any other special character, for example -



                                                                    var a = 1 : 3 ;



                                                                    Now if you try to access in like a.1 it will through an error, because it is expecting an string over there.






                                                                    share|improve this answer

























                                                                      0












                                                                      0








                                                                      0







                                                                      Case where notation is helpful :



                                                                      If your object is dynamic and there could be some random values in keys like number and or any other special character, for example -



                                                                      var a = 1 : 3 ;



                                                                      Now if you try to access in like a.1 it will through an error, because it is expecting an string over there.






                                                                      share|improve this answer













                                                                      Case where notation is helpful :



                                                                      If your object is dynamic and there could be some random values in keys like number and or any other special character, for example -



                                                                      var a = 1 : 3 ;



                                                                      Now if you try to access in like a.1 it will through an error, because it is expecting an string over there.







                                                                      share|improve this answer












                                                                      share|improve this answer



                                                                      share|improve this answer










                                                                      answered Dec 9 '16 at 13:42









                                                                      AnshulAnshul

                                                                      6,16464263




                                                                      6,16464263





















                                                                          0














                                                                          Let me add some more use case of the square-bracket notation. If you want to access a property say x-proxy in a object, then - will be interpreted wrongly. Their are some other cases too like space, dot, etc., where dot operation will not help you. Also if u have the key in a variable then only way to access the value of the key in a object is by bracket notation. Hope you get some more context.






                                                                          share|improve this answer



























                                                                            0














                                                                            Let me add some more use case of the square-bracket notation. If you want to access a property say x-proxy in a object, then - will be interpreted wrongly. Their are some other cases too like space, dot, etc., where dot operation will not help you. Also if u have the key in a variable then only way to access the value of the key in a object is by bracket notation. Hope you get some more context.






                                                                            share|improve this answer

























                                                                              0












                                                                              0








                                                                              0







                                                                              Let me add some more use case of the square-bracket notation. If you want to access a property say x-proxy in a object, then - will be interpreted wrongly. Their are some other cases too like space, dot, etc., where dot operation will not help you. Also if u have the key in a variable then only way to access the value of the key in a object is by bracket notation. Hope you get some more context.






                                                                              share|improve this answer













                                                                              Let me add some more use case of the square-bracket notation. If you want to access a property say x-proxy in a object, then - will be interpreted wrongly. Their are some other cases too like space, dot, etc., where dot operation will not help you. Also if u have the key in a variable then only way to access the value of the key in a object is by bracket notation. Hope you get some more context.







                                                                              share|improve this answer












                                                                              share|improve this answer



                                                                              share|improve this answer










                                                                              answered May 1 '17 at 4:43









                                                                              Manish WaranManish Waran

                                                                              596




                                                                              596















                                                                                  protected by Samuel Liew Oct 5 '15 at 9:02



                                                                                  Thank you for your interest in this question.
                                                                                  Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).



                                                                                  Would you like to answer one of these unanswered questions instead?



                                                                                  Popular posts from this blog

                                                                                  Kleinkühnau

                                                                                  Makov (Slowakei)

                                                                                  Deutsches Schauspielhaus