Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript Node.js Basics 2017 Building a Command Line Application Parsing JSON

Hamzah Iqbal
seal-mask
.a{fill-rule:evenodd;}techdegree
Hamzah Iqbal
Full Stack JavaScript Techdegree Student 11,145 Points

At 2:00, why do we not return length on the JavaScript object. We are returning a number after all?

As the question states, why do we not return xxx.points.JavaScript.length? It is a number after all? And also, why do we use console.dir, when console.log returns the same ?

3 Answers

James Crosslin
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
James Crosslin
Full Stack JavaScript Techdegree Graduate 16,882 Points

If you were to log profile.points.JavaScript.length you would get a return of 4, because his point total had 4 digits. If you place a console log in your code as so:

//Require http Module
const https = require("https")
const username = "chalkers"

function printMessage(username, badgeCount, points) {
  const message = `${username} has ${badgeCount} badges and ${points} points in JavaScript`
  console.log(message)
}
//Connect to the API URL (https://teamtreehouse.com/username.json)

const request = https.get(
  `https://teamtreehouse.com/${username}.json`,
  (response) => {
    let body = ""
    // Read the data
    response.on("data", (data) => {
      body += data.toString()
    })
    response.on("end", () => {
      const profile = JSON.parse(body)
      console.log(profile)      // <------ add this to log your profile object
      printMessage(username, profile.badges.length, profile.points.JavaScript)
    })
    // Parse the data

    // Print the data
  }
)

you will see that the points portion of the returned object looks like this:

points: {
    total: 24554,
    HTML: 1948,
    CSS: 1522,
    Design: 566,
    JavaScript: 5991,
    Ruby: 1950,
    PHP: 1041,
    Android: 493,
    'Development Tools': 1296,
    Business: 414,
    Python: 446,
    Java: 750,
    'Digital Literacy': 468,
    'C#': 225,
    Databases: 576,
    'Data Analysis': 56,
    APIs: 107,
    Security: 0,
    Go: 0,
    'Quality Assurance': 0,
    'Machine Learning': 0,
    'Learning Resources': 0,
    'Computer Science': 0,
    'Equity, Diversity, and Inclusion (EDI)': 0,
    '21st Century Skills': 0
  }

That's why we do profile.points.JavaScript instead of profile.points.JavaScript.length. We want the number of points, not the length of the number.

For your second question, the console.log() method prints out a toString() representation of the object in the console to the user. The console.dir() method outputs the list of object properties of a specified object in the console to the user, in a format we recognize as an object literal.

If you use a text editor like VSCode for your terminal or console, then you won't see a difference in display. There would likely be a difference in format in an unaltered native terminal however.

console.log and console.dir are most of the time the same, console.dir used to be better for long lists or objects, as it was intended for that, while log was intended for shorter messages.

Nowadays, there's rarely a difference.