Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Mar 2013
    Posts
    81
    Thanks
    45
    Thanked 0 Times in 0 Posts

    Why = and not : ?

    As the title says why do we use = to assign variable a value and not :
    Fr instance here, in this function.
    Why cannot colon be used instead of equal sign..


    packet=new Object();
    packet.color="red";
    packet.type="plastic";
    packet.size=function(a,b){
    return a+b;
    }

    document.write(packet.size(10,10));

  • #2
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,460
    Thanks
    11
    Thanked 600 Times in 580 Posts
    why is grass green?
    because it has to be some color, and green looks decent.

    but also, the ":" char is used for labels in JS, even if JS doesn't often use labels.
    my site (updated 2014/10/20)
    BROWSER STATS [% share] (2014/9/03) IE7:0.1, IE8:4.3, IE11:9.2, IE9:2.7, IE10:2.6, FF:16.8, CH:47.5, SF:7.8, NON-MOUSE:37%

  • Users who have thanked rnd me for this post:

    h123er2 (10-24-2013)

  • #3
    Regular Coder
    Join Date
    Aug 2012
    Posts
    145
    Thanks
    0
    Thanked 26 Times in 26 Posts
    i found two answers that may help with this, but are more focused on the ':' rather than '='

    http://stackoverflow.com/questions/7...ript-represent
    http://stackoverflow.com/questions/4...-in-javascript

    Now based on one of the answers i'm not certain if this would work but i changed your original code to be more based on ':" via object literal:

    Code:
    <!DOCTYPE html>
    <html>
    <head>
    
    </head>
    <body>
    
    
    <p id="demo"></p>
    
    <script>
    var packet = {
    color:'red',
    type:'plastic',
    size:function(a,b){return a+b}
    };
    
    document.getElementById('demo').innerHTML = packet.size(20,20);
    document.getElementById('demo').style.background = packet.color;
    
    </script>
    </body>
    </html>
    1 Corinthians 15:3-4 / Ephesians 2:8-9 - What or Who are you living for? Jesus is returning very soon

  • #4
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,642
    Thanks
    0
    Thanked 649 Times in 639 Posts
    It depends on where you are doing the asignment whether you should use = or : to do the assignment in JavaScript.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •