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

Renaming Python Names for JavaScript

Renaming Python Names for JavaScript

In this lesson, you gave the release note’s names their JavaScript spelling, and saw why: JavaScript runs Python’s names, but its readers expect their own.

  • A line-by-line port that keeps every Python name runs and prints the same header as Python. Underscores are allowed in JavaScript names, as the settings show. JavaScript doesn’t check a name’s style; whoever reads the file next does, and to them snake_case reads as Python in JavaScript syntax.
  • For variables and functions, JavaScript uses camelCase where Python uses snake_case: the first word lowercase, and each word after it capitalized. open_bugs becomes openBugs, and print_header becomes printHeader, where it’s declared and where it’s called.
  • Classes, when you meet them, use PascalCase, like Python’s CapWords, and a JavaScript name can hold a dollar sign, which you’ll see in library names but shouldn’t use for your own variables.
  • The six settings keep their capitals: many projects write top-level settings that someone edits in UPPER_SNAKE_CASE. Python enforces nothing, so there the capitals are the only signal not to change a value.
  • In JavaScript, const stops a name from being reassigned, so openBugs is a const too, in camelCase. The capitals only mark the settings, the values someone edits before a run.
  • In JavaScript, default is a reserved word, kept for the language’s own syntax, as Python keeps if and class. delete, also ordinary in Python, is another. Using one as a name stops the file with a SyntaxError, so the port names the client’s channel defaultChannel.
  • JavaScript doesn’t mind underscores in a file name, but many projects use kebab-case, lowercase words joined by hyphens: release_note.py becomes release-note.js.

Here’s the top of the Python script, with the header’s channel lines:

Language: Python
CLIENT = "Fernleaf Bakery"
VERSION = "2.4.1"
DEPLOYED_BY = "Maya"
BUGS_OPENED = 9
BUGS_CLOSED = 7
CODE_FREEZE = True

open_bugs = BUGS_OPENED - BUGS_CLOSED

def print_header():
    print(f"Release {VERSION} for {CLIENT}")
    print(f"Open bugs: {open_bugs}")
    default = "#fernleaf-bakery"
    print(f"Channel: {default}")

print_header()

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:02 This line-by-line port of the Python release note keeps every name, even the file’s. It runs, and prints the same header as Python. Underscores are allowed in JavaScript names, as the settings have shown all along.

00:18 So why rename?

00:21 JavaScript doesn’t check a name’s style. Like snake case in Python, camel case is a convention, not a rule. Whoever reads the file next does, and to them, snake case reads as Python in JavaScript syntax.

00:36 In this lesson, you’ll give each name its JavaScript spelling.

00:42 For variables and functions, JavaScript uses camel case where Python uses snake case: the first word lowercase, and each word after it capitalized. open_bugs drops its underscore and gains a capital B.

00:57 print_header() changes the same way, where it’s declared and where it’s called.

01:03 Classes, when you meet them, use Pascal case, like Python’s CapWords, and a JavaScript name can hold a dollar sign, which you’ll see in library names but shouldn’t use for your own variables.

01:16 The six settings keep their capitals. Many projects write top-level settings that someone edits in upper snake case. Here the analogy stops. Python enforces nothing, so there the capitals are the only signal not to change a value.

01:34 In JavaScript, const stops a name from being reassigned, so openBugs is a const too, in camel case. The capitals only mark the settings, the values someone edits before a run.

01:48 The header ends by naming the client’s channel default, an ordinary name in Python. In JavaScript, default is a reserved word, kept for the language’s own syntax, as Python keeps if and class.

02:04 delete, also ordinary in Python, is another. Using one as a name stops the file with a SyntaxError. So the port renames it defaultChannel.

02:16 Last, the file name. JavaScript doesn’t mind underscores in a file name, as the first run showed, but most projects use kebab case: lowercase words joined by hyphens.

02:29 Run the renamed file: the same header as Python, now with its channel. That’s the JavaScript way: rename as you port. Camel case for names, capitals for the settings and kebab case for the file, so it reads as JavaScript, not Python.

02:48 Next up is Letting a Formatter Decide the Style, a text lesson.

Become a Member to join the conversation.