JavaScript: why colon (:) and not equals (=)

fuzzybabybunny

Moderator<br>Digital & Video Cameras
Moderator
Jan 2, 2006
10,455
35
91
I'm starting to learn JS and I've noticed this that makes me frustrated:

Code:
var myStruct = {
   myValue: "value"
};

WTF?

So you're setting the variable myStruct EQUAL to the stuff in the brackets.

But then you're also setting myValue EQUAL to "value"

So why use the colon? Wouldn't it be more consistent, sensicle, and intuitive to just do:

Code:
var myStruct = {
   myValue = "value"
};
 
  • Wow
Reactions: DAPUNISHER

Markbnj

Elite Member <br>Moderator Emeritus
Moderator
Sep 16, 2005
15,682
13
81
www.markbetz.net
Last edited:

douglasb

Diamond Member
Apr 11, 2005
3,163
0
76
Mark is right. It's just the standard for JSON, so everything is listed as key/value pairs.
When you think of it that way, it would look kind of strange to transmit JSON objects full of equal signs. Especially when you start having objects inside of objects. It would look something like this:

Code:
var someObject = {
   string1 = "value",
   nestedObject = {
       string2 = "foo",
       someOtherString = "whatver",
       someNumber = 5,
       yetAnotherNestedObject = {
           string3 = "this looks ugly...",
           string 4= "really ugly."
       }
   },
   string5 = "etc"
};

Maybe it's just because I'm used to it, but I find the colons much more readable when I am glancing at JSON. It just seems to translate better to "key: value", at least in my mind.
 

ejjpi

Member
Dec 21, 2013
58
0
0
pangoly.com
You are using the javascript object literal notation, so writing:

Code:
var myStruct = {    myValue: "value" };
It's exactly the same as writing:

Code:
var myStruct = new Object();
myStruct.myValue = "value";
So why the object literal notation has become a standard way to define objects in js? Because it's shorter, it's easier to create more structured objects on the fly and there is no scope resolution.

And please, do not try to make any further comparison between those 2 languages. One is a scripting language, the other is an imperative procedural language. It doesn't make any sense to compare a dog with a car...

I hope to have clarified some things up :)
 
Last edited:

mosco

Senior member
Sep 24, 2002
940
1
76
And this notation isn't just used in javascript or json, it's a common way to express the key value pair construct.

In objective-c for example you could do:

Code:
NSDictionary *dictionary = @{ @"value" : @YES }
 

Markbnj

Elite Member <br>Moderator Emeritus
Moderator
Sep 16, 2005
15,682
13
81
www.markbetz.net
And this notation isn't just used in javascript or json, it's a common way to express the key value pair construct.

In objective-c for example you could do:

Code:
NSDictionary *dictionary = @{ @"value" : @YES }

Good point. Same syntax for dictionaries in Python:

Code:
colors = {'red':0xff0000, 'blue':0x0000ff}
 

mv2devnull

Golden Member
Apr 13, 2010
1,495
143
106
C/C++ has = for assignment of a value and == for testing for equality. That leads to trivial errors too. Whose syntax has the := for assignment operator?
 

h01mes

Junior Member
Aug 5, 2017
1
0
1
As explained in this post, ":" is used in a dictionary that is a very similar to a class/struct. However, you can "=" in a constructor that creates a more "real" object:

Code:
    function myStruct(value) {
      this.myValue = value;
    }
 

devBunny

Junior Member
Sep 12, 2017
21
5
6
You apes just don't get it. :p My fellow bunny didn't ask "What am I doing?" or "How do I do this?". The question was "Why does javascript they require it this way?", coupled with the observation that it Just. Doesn't. Make. Sense.

fuzzybabybunny is a novice javascripter and I'm a veteran javascripter and we both think alike on this.

I have no answer to "Why?" but I can also see no reason why the syntax for object initialisation has to use colons instead of equals. The parsing would work whichever token was used. I'd go further and say that it doesn't need to use commas instead of semicolons to terminate a property initialisation.

For me, this crops up every now and then when I'm coding a new area of functionality. I'll start with some code which sets up some variables and then go on to use them. As the functionality grows I might add some more variables and then at some point it'll look like it may be handier to have them in an object (which may, further down the road, grow into a class).

I then have to turn something that looks like
Code:
var foo = 6295141.3;
var bar = "Right you are";
var the = "Wednesday";
var elephant = 0.5;
into
Code:
var obbjy = {
    foo: 6295141.3,
    bar: "Right you are",
    the: "Wednesday",
    elephant: 0.5
    };

Now wouldn't it be a lot easier if the object initialisation used equals and the terminator was semicolon? Then, rather than faffing about editing each and every assignment, all I'd need to do would be to take out the vars, pop some braces around the lot and assign that to the new object variable.


ps. Anyone talking about JSON is barking up the wrong tree. JSON is relatively new. It's a subset of javascript but it has nothing to with javascript's design. The only design decision of JSON is the requirement for inefficiency, namely the use of quotes around property names that do not need them. { "UnnecessaryQuotes": true }
 

alexleduc9001

Junior Member
Aug 29, 2022
1
0
6
You apes just don't get it. :p My fellow bunny didn't ask "What am I doing?" or "How do I do this?". The question was "Why does javascript they require it this way?", coupled with the observation that it Just. Doesn't. Make. Sense.

fuzzybabybunny is a novice javascripter and I'm a veteran javascripter and we both think alike on this.

I have no answer to "Why?" but I can also see no reason why the syntax for object initialisation has to use colons instead of equals. The parsing would work whichever token was used. I'd go further and say that it doesn't need to use commas instead of semicolons to terminate a property initialisation.

For me, this crops up every now and then when I'm coding a new area of functionality. I'll start with some code which sets up some variables and then go on to use them. As the functionality grows I might add some more variables and then at some point it'll look like it may be handier to have them in an object (which may, further down the road, grow into a class).

I then have to turn something that looks like
Code:
var foo = 6295141.3;
var bar = "Right you are";
var the = "Wednesday";
var elephant = 0.5;
into
Code:
var obbjy = {
    foo: 6295141.3,
    bar: "Right you are",
    the: "Wednesday",
    elephant: 0.5
    };

Now wouldn't it be a lot easier if the object initialisation used equals and the terminator was semicolon? Then, rather than faffing about editing each and every assignment, all I'd need to do would be to take out the vars, pop some braces around the lot and assign that to the new object variable.


ps. Anyone talking about JSON is barking up the wrong tree. JSON is relatively new. It's a subset of javascript but it has nothing to with javascript's design. The only design decision of JSON is the requirement for inefficiency, namely the use of quotes around property names that do not need them. { "UnnecessaryQuotes": true }
Agreed with the above and will add that that just about everyone can quickly do Ctrl+d to set multi cursors on every sequence of " =" (VS Code) and then change it all by typing a single ":". I'm sure other editors have similar shortcuts. There are a tone of productivity tools for working with text, and changing equals for colons is rendered insignificant thanks to them.
 

reallyscrued

Platinum Member
Jul 28, 2004
2,617
5
81
Everyone is providing really technical examples but I think an analogy and history would be better.

JavaScript's spec says the equal sign will be used as an assignment operator.
JSON's spec says the colon will be used to denote key/value pairs.

These are two different specs, and they serve two different purposes.

You're kinda asking why "cat = gato" isn't just "cat = cat" - the grammar and spelling of the two specs being different is definitely something that is a possibility, so it shouldn't be *such* a surprise right off the bat.

So the question then is naturally: why did the spec for one choose assignment operator to be equals and the other a colon for nearly the same idea?

JavaScript is a C-based language developed in 1995. Assigning variables with the equals operator dates back to very early programming and that syntax simply was carried forward.

JSON was developed in around the year 2000 (and thus, not in tandem with JS and so they didn't just switch up operators on you during the same period of development if that's what you thought).

And historically, key/value pairs were denoted with colons and that's was carried forward.
See: https://en.wikipedia.org/wiki/JSON for their goals and requirements, but it definitely wasn't chosen this way to be intentionally confusing.

Further - I think semantically a colon makes more sense in JSON. DevBunny is correct that any token can be used when parsing and in the design but when thinking about it what it actually means - in the programmatic sense, you are assigning a value to a variable - that's historically sound. Equals operator.

But in JSON, you are declaring some attribute of something is a particular value.
 
Last edited: