Code Monkey home page Code Monkey logo

phase-0-pac-2-default-sequence-and-comments's Introduction

Default Sequence and Comments

Learning Goals

  • Recognize the comment marker
  • Add a comment to code
  • 'Comment out' code

Introduction

As said in the introduction to this section:

JavaScript by default will read our code according to the rules of a default sequence or default flow: "every line, top to bottom, left to right as ruled by order of operations."

Using SELECTION statements we can make JavaScript "skip" over code if some Boolean evaluation is (or is not) true. Using REPETITION statements, we can make JavaScript "stay put" on one line and do it over and over until some Boolean evaluation is (or is not) true. The only way to make JavaScript "not see" a line without a Boolean evaluation at play is to "hide" it from JavaScript using a comment.

Be sure to "play along" with the examples below by keying in this code into repl.it. We need to build comfort with working along with the lessons.

Recognize the Comment Marker

We can exclude a line from the default sequence by starting the line with the comment marker: //. After JavaScript sees a //, it will ignore from the // to the next line.

Be careful! A comment placed in the middle of an expression can confuse JavaScript.

// Don't do this:
const sum = ( 1 //+ 1);

As a rule of thumb, try to comment out whole lines only (i.e., place the comment marker at the beginning of the line). As you get more comfortable with JavaScript, you might find clever ways to use comments, but best keep things simple for now.

Add a Comment to Code

Comments are primarily used to provide references or explanations about what's going on in code.

// Perform a constant expression evaluation
3;
// Assign constant 3 to variable triangleSides
const triangleSides = 3;

Comments such as these are not particularly helpful. They're just restating what the code does. More often we add comments with motivation, or references, or blog posts, or bug reports:

// From the Three Dog Night song: "Joy to the World (Jeremiah was a Bullfrog)"
const lineOne = "Joy to the world";
const lineTwo = "All the boys and girls";
const lineThree = "Joy to the fishes in the deep blue sea";
const lineFour = "Joy to you and me";

// The '\n' inserts a new line into the string
const chorus = `${lineOne}\n${lineTwo}\n${lineThree}\n${lineFour}`;

chorus;

A-HA! Moment. Recall that the "return value" documentation shorthand //=> starts with a comment marker. This indicates that what's after // is not part of the code itself. That's why it's used as an "in-code" documentation convention.

Comment Out code

Another way to use comments is to "comment out" code, to "hide" or "mute" buggy or unused code from the default sequence.

With our current code:

const lineOne = "Joy to the world";
const lineTwo = "All the boys and girls";
const lineThree = "Joy to the fishes in the deep blue sea";
const lineFour = "Joy to you and me";

const chorus = `${lineOne}\n${lineTwo}\n${lineThree}\n${lineFour}`;

chorus;

Default sequence satisfies our expectations by returning:

Joy to the world
All the boys and girls
Joy to the fishes in the deep blue sea
Joy to you and me

Now let's "comment out" lineThree. We'll also need to modify chorus accordingly. (If you aren't sure why, try just commenting out lineThree in your REPL and see what happens when you run the code.) We'll comment out the current version of chorus to save it and modify a copy:

const lineOne = "Joy to the world";
const lineTwo = "All the boys and girls";
//const lineThree = "Joy to the fishes in the deep blue sea";
const lineFour = "Joy to you and me";

// const chorus = `${lineOne}\n${lineTwo}\n${lineThree}\n${lineFour}`;
const chorus = `${lineOne}\n${lineTwo}\n${lineFour}`;

chorus;

If we run this code, it returns:

Joy to the world
All the boys and girls
Joy to you and me

If we want to go back to the original version, we simply "comment back in" lineThree and the original chorus variable, and comment out or delete the modified version of chorus.

If we want to comment out multiple lines, we can either place the comment marker at the beginning of each line, or we can wrap the lines with /* and */:

/*
const lineOne = "Joy to the world";
const lineTwo = "All the boys and girls";
const lineThree = "Joy to the fishes in the deep blue sea"; 
*/
const lineFour = "Joy to you and me";

lineFour;

It's common for developers to test two code paths (in effect, doing a selection statement's work by hand!) by "commenting out" and "commenting back in" code.

Conclusion

The default sequence is how JavaScript reads and executes each of the statements and commands in JavaScript code. To "hide" a line of code from being seen by the JavaScript engine, start the line with the comment marker //. We use comments to provide lightweight documentation or to hide code while we debug or test it.

phase-0-pac-2-default-sequence-and-comments's People

Contributors

lizbur10 avatar sgharms avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.