Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

From print() to console.log()

From print() to console.log()

In this lesson, you ported the setup check’s lines to a JavaScript file, ran it with node, and found out what happens to print()’s keyword arguments.

  • Create a folder called onboarding-check in your home folder, save the port in it as a new file with the .js extension, check-setup.js, and run it from that folder with node and its name, as you run a Python script with python: node check-setup.js. Node runs the file from top to bottom.
  • print() becomes console.log(). It puts a space between several values and prints strings without quotes, as print() does.
  • Node’s process object holds the setup values, with no import: process.versions.node, process.platform, and process.cwd().
  • JavaScript has no keyword arguments, so sep= and end= don’t carry over. For the report’s newline-separated steps, give each step its own console.log() call.
  • console.error() writes to stderr, like print(..., file=sys.stderr).
  • JavaScript has no __name__, so a Python main guard doesn’t port. Keep the lines inside it.

Watch out for print() out of habit. JavaScript has no print() function, so a call to it stops the script at that line.

The team’s Python check prints its next steps with one print() and sep="\n":

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

00:00 In this lesson, you’ll port the setup check’s lines to a JavaScript file and run it with node. At the REPL, you tried one line at a time. The check itself is a script, and in Python, you run it with python and the file’s name.

00:15 It prints your Python version, your operating system, and your current folder, then the team’s next steps.

00:23 Now port the three lines. Each print() becomes console.log(), and the values inside stay as they are. Python imports three modules for these values.

00:35 Node keeps all three on its process object, which every script can use without an import. Watch out for print() out of habit. JavaScript has no print() function, so a script that calls it stops there with an error.

00:51 Create a folder called onboarding-check in your home folder, and save the port in it as a new file with the .js extension, check-setup.js.

01:00 Then, in that folder, run it the way you ran the Python: node check-setup.js. The same three lines come out, with Node’s version in place of Python’s.

01:13 console.log() puts a space between its values, as print() does, and prints strings without quotes. And there’s no undefined this time: that echo was the REPL’s.

01:24 Node runs the file from top to bottom, as Python does. JavaScript has no __name__, so a Python main guard doesn’t port.

01:35 Now for the team’s next steps. The Python check prints all three with one print() call, and sep set to a newline puts each on its own line.

01:45 Port it the same way: swap print() for console.log() and keep the rest. What will JavaScript print for the steps? Your turn: pause the video and predict.

02:01 One line: the three steps run together, then a blank line, and no error.

02:10 Why? JavaScript has no keyword arguments. It reads sep="\n" as an assignment: it sets a name, sep, to a newline, and passes that newline along as a fourth value.

02:24 console.log() puts a space between its values, so the steps share one line, and the newline at the end adds the blank one. Python’s end= does the same: it sets a name and becomes one more value.

02:38 And in a plain script like this one, the new name raises no error. A later lesson shows when it does.

02:47 The JavaScript way is to give each line its own call. Each step gets a console.log(), and sep goes. Run it again, and the steps print on three lines, as Python’s do.

03:01 The Python check ends with a reminder to paste the report, sent to standard error with the file keyword. In JavaScript, the options of console.log() aren’t keywords.

03:12 They’re other methods on the same console object, and console.error() writes to standard error. Send the report to a file, and in both languages only the reminder stays in the terminal.

03:26 So here’s the rule. Port print() to console.log() value for value, but never keyword for keyword: one console.log() per line, and console.error() for anything that isn’t the report.

03:41 Next up is the exercise Report the Setup Values, where you port a Python report of the setup values and the next steps on your own.

Become a Member to join the conversation.