console.log double posting

+2 Ray LeDuc · March 15, 2015
Hey guys I could use some help. I am just learning about objects within JavaScript and was trying a simple script which runs and gives the expected results with the exception of  it posting twice. I am sure there is a logical explanation as to why but I just don't know why.


var ray = { 
    name: "ray",
    skills: ["JavaScript", "HTML", "CSS" ],
    "favorite color": "blue"
    };

console.log(ray["name"]);
console.log(ray["favorite color"])

so when I refresh the browser it will print the name and favorite color each on a separate line but will do it twice. Any Idea as to why. please help, I am finding my self a bit annoyed by it.

Post a Reply

Replies

Oldest  Newest  Rating
0 Doug Fresh · March 27, 2015
@ Ray, 
Yep, I see the Parenthesis I missed, lol thanks. I'm glad you were able to solve the problem. 

@ Sid,
Yes, programming conventions suggest to use a defined attribute rather than a "magic" attribute or dynamically created attribute. 
0 Sid Ahmed Brahimi · March 27, 2015
Hello,
and you should  delete the ("") from the property because it's for values like "blue"

favorite color it will look like:
favorite color : ''blue';
0 Ray LeDuc · March 21, 2015
Hey Doug,

     Thanks for the reply and I use chrome with their dev tools for my js. secondly I didn't put those links their intentionally but needless to say they are there. after applying your code and tried it out I had issues to get it to work however, I found and fixed it and it worked just fine. there was a missing ) but no biggie easy enough to spot and fix. 

Thanks again for your reply and assistance.
Ray  L
+1 Doug Fresh · March 19, 2015
Are you using another piece of software to check your JS code, such as Firebug? That may be the issue. I tried a few versions of the code you supplied above, with some minor changes. You'll note I didn't use the html links in my code, this may be a part of your issue as well.
You'll notice the syntax of my code is slightly different, as I prefer dot notation when available. This is to eliminate possible issues and use proper conventions with the code.

// This section creates an Object called 'ray'
var ray = {
name: "ray",
skills: ["JavaScript", "HTML", "CSS" ],
"favorite color": "blue"
};
//end object

// begin console logs
console.log(ray.name);
console.log(ray.skills);
console.log(ray["favorite color"];
//end log

The only other thing I can think of, is to check your HTML and see if the <script src="'whatever your JS name is'.js" > is listed twice, causing the procedure to run twice.
Hope this helps, and if it doesn't, please feel free to ask questions!
  • 1

Javascript

106,956 followers
About

A scripting language that is added to standard HTML to create interactive effects, apps, games for the browser.

Links
Moderators