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. Give each line its own console.log() instead.
  • 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 the script stops at that line with ReferenceError: print is not defined.

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:01 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:18 It prints your Python version, your operating system, and your current folder, then the team’s next steps.

00:26 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:39 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:56 Save the port in a folder of its own, and run it the way you ran the Python: node, then the file’s name. The same three lines come out, with Node’s version in place of Python’s.

01:09 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:22 Node runs the file from top to bottom, as Python does. JavaScript has no __name__, so a Python main guard doesn’t port. Now for the team’s next steps.

01:35 The Python check prints all three with one print() call, and sep set to a newline puts each on its own line. Port it the same way: swap print() for console.log() and keep the rest.

01:49 What will JavaScript print for the steps? Your turn: pause the video and predict.

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

02:08 Why? JavaScript has no keyword arguments.

02:14 It reads sep="\n" as an assignment: it sets a name, sep, to a newline, and passes that newline along as a fourth value. console.log() puts a space between its values, so the steps share one line, and the newline at the end adds the blank one.

02:33 Python’s end= does the same: it sets a name and becomes one more value. And in a plain script like this one, the new name raises no error. A later lesson shows when it does.

02:48 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:03 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:15 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:30 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:45 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.