Program Structure
Explore the fundamental concepts of JavaScript program structure by understanding statements, how code executes line by line, and the role of comments. This lesson helps you grasp essential practices for writing clear and organized JavaScript programs and introduces you to the syntax and flow of basic code execution.
We'll cover the following...
We already defined a computer program as a list of commands telling a computer what to do. These orders are written as text files and makeup what’s called the “source code” of the program. The lines of text in a source code file are called lines of code. The source code may include empty lines: these will be ignored when the program executes.
Statements
Each instruction inside a program is called a statement. A statement in JavaScript usually ends with a semicolon (albeit it’s not strictly mandatory). Your program will be made up of a series of these statements.
You usually write only one statement per line.
Execution flow
When a program is executed, the statements in it are “read” one after another. It’s the combination of these individual results that produce the final result of the program.
Here’s an example of a JavaScript program including several statements, followed by the result of its execution.
Depending on your work environment, the execution result may not include quotes around text.
As expected, a division by zero (12/0) results in an infinity value.
Comments
By default, each line of text in the source files of a program is considered a statement that should be executed. You can prevent certain lines from executing by putting a double slash before them: //. This turns the code into a comment.
During execution, the commented-out lines no longer produce results. As we hoped, they weren’t executed. Comments are great for developers so you can write comments to yourself, explanations about your code, and more, without the computer actually executing any of it. You can also write comments by typing /* */ around the code you want commented out.
Comments are a great source of info about a program’s purpose or structure. Adding comments to complicated or critical parts is a good habit you should build right now!