CodingNic

Variables, Data Types & Operators

Output, Comments, and Conventions

Variables, Data Types & Operators 15 min read

Output, Comments, and Conventions

Objectives

By the end of this chapter, you should be able to:

  • Use console.log() and its siblings console.error() and console.table()
  • Write single-line and block comments
  • Follow basic naming and coding conventions
  • Explain what "use strict" changes in practice

๐Ÿ’ก Why this matters: You’ve been using console.log() since Module 1. This lesson rounds out how you show output, document your code, and write it the way experienced JavaScript developers do.

console.log() and Its Siblings

console.log() prints a value to the terminal (or the browser console). You can pass it more than one value at a time, separated by commas.

javascript
console.log("Score:", 95);
// Score: 95

console.error() works the same way, but marks the output as an error. In a terminal, error output goes to a separate stream (called “stderr”) from regular output, which is why tools and log viewers can highlight or filter it separately.

javascript
console.error("Something went wrong");
// Something went wrong

console.table() prints structured data as an actual table, which is far easier to scan than a wall of text once you have more than one record. Give it an array where each entry is an object with the same shape:

javascript
console.table([
  { name: "Erin", score: 90 },
  { name: "Maya", score: 85 },
]);
text
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚ (index) โ”‚ name   โ”‚ score โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚ 0       โ”‚ 'Erin' โ”‚ 90    โ”‚
โ”‚ 1       โ”‚ 'Maya' โ”‚ 85    โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

Arrays and objects are covered properly in a later module. For now, just recognize console.table() as a tool worth reaching for whenever you’re logging a list of similarly-shaped records.

Comments

A comment is text in your code that JavaScript ignores when it runs. Comments explain your code to other people, and to yourself later.

A single-line comment starts with // and runs to the end of the line:

javascript
// This calculates the total price including tax
const total = 19.99 * 1.08;

A block comment starts with /* and ends with */, and can span multiple lines:

javascript
/*
  This section handles the user's score.
  It starts at zero and increases as they answer questions.
*/
let score = 0;

Use comments to explain why something is done a certain way, not to restate what the code already says clearly.

javascript
// Bad: just repeats the code
let x = 5; // set x to 5

// Good: explains the reasoning
let retryLimit = 5; // API rejects more than 5 retries per minute

Naming and Coding Conventions

You already met camelCase for variable names in an earlier lesson. A few more conventions keep JavaScript code consistent and easy to read across projects:

  • Variable and function names use camelCase: firstName, calculateTotal.
  • Names should describe what the value holds, not how it’s used internally: userAge rather than a or temp.
  • Use const unless you specifically need to reassign, then use let.
  • Add a space around operators: total + 1, not total+1.
  • End statements with a semicolon.
javascript
// Follows convention
const firstName = "Sam";
const isActive = true;

// Hard to read, avoid this style
const x=false,y="sam"

Consistency matters more than any single rule. Following the same style throughout a file makes it easier for anyone (including you) to read later.

Strict Mode

Adding "use strict" as the very first line of a file (or function) turns on strict mode, which makes JavaScript enforce rules that are normally relaxed. The clearest example: assigning to a variable you never declared.

Without strict mode, this runs without complaint and quietly creates a global variable:

javascript
score = 100;
console.log(score);
// 100

With "use strict" at the top of the file, the same code throws an error instead:

javascript
"use strict";

score = 100;
console.log(score);
// ReferenceError: score is not defined

That error is a good thing. It catches a typo (forgetting const or let, or misspelling a variable name) immediately, instead of letting a silent global variable cause a confusing bug somewhere else in your program. Many tools and modern JavaScript features apply strict mode automatically, but it’s worth knowing what "use strict" does and adding it yourself in plain scripts.

A note on user input: this module doesn’t cover asking the user for input while your program runs. That comes later, once you’re working with a browser and forms.

Try It

  1. Log two values in one console.log() call: a name and an age.
  2. Use console.error() to log a message like "Invalid input".
  3. Build an array of two or three objects representing people (name and age) and print them with console.table().
  4. Write one single-line comment and one block comment explaining a variable of your choice.
  5. Write a short script that starts with "use strict", then assigns to a variable you never declared. Run it and read the error.

Recap

  • console.log() prints output; console.error() marks it as an error; console.table() prints structured data as a table.
  • // starts a single-line comment; /* */ wraps a block comment.
  • CamelCase names, const by default, and consistent spacing keep code readable.
  • "use strict" makes JavaScript throw on mistakes it would otherwise ignore silently, like assigning to an undeclared variable.

Next lesson: exercises that pull together everything from this module.