Chapter 2 - Program Structure (Part 1)

Chapter 2 - Program Structure (Part 1)

·

3 min read


In this chapter, Marijn Haverbeke delves into the realm of programming with JavaScript.

Expressions and Statements

An expression in code is a piece of code that results in a value. Expressions can be as simple as a number like 22 or a string like "psychoanalysis”. An expression between parentheses is also an expression.

Statements in programming serve as instructions that tell the computer what to do. They utilize expressions, which are smaller tasks or calculations, to carry out specific actions. A program is a collection of these statements, guiding the computer on how to execute tasks and solve problems.

In other words, an expression is a piece of code that produces a value. A statement is a line of code that does something that has an impact on the real world, like displaying something on a screen or changing the internal state of the machine. The change in the outside world is called a side effect.

In some cases, JavaScript allows you to skip the semicolon at the end of a statement. Using semicolons always causes a debate in the JavaScript community. There are good arguments that support using semicolons. But there are also great reasons why you should not use them. However, the author of this book advises readers to use semicolons as he does throughout the book, as it is considered a best practice.

Bindings/Variables

Sometimes values that we get in a program need to be stored internally. So JavaScript uses bindings or variables to achieve this. However, a variable functions more like a tentacle than a box; it grasps or holds a value rather than storing it.

In JavaScript, you can create a variable using the keywords let, const, or var.

Example: let caught = 5 * 5; creates a variable named caught.

Once the variable is defined, its name can be used as an expression to access the value it holds. Also, a variable’s value can be changed by using the ‘=’ operator, but this operation only works for let and var.

Example:

let mood = "light";
console.log(mood); // → light
mood = "dark";
console.log(mood); // → dark

Variable Names

Variable names in JavaScript can include letters, numbers, dollar signs ($), or underscores (_), but no other punctuation or special characters. It is important to note that a variable name must begin with a letter, and certain reserved keywords in JavaScript such as 'if', 'else', and 'function' cannot be used as variable names.

The Environment

The environment in computer programming is the collection of variables and their values that exist at a specific time of program execution. That means an environment is not empty when the program starts; it already contains some variables and values. It also includes variables that allow the program to interact with the surrounding system, like reading mouse and keyboard input. So the environment facilitates the smooth operation of programs by providing necessary resources and connections both within the program and with the external system.

Functions:

In the default environment of programming, many values are actually functions. Now, what's a function? A function is a piece of a program wrapped in a value that we can carry around like a little package, ready to use whenever we need it. And when we want to make that code do its thing, we apply it or run it.

Conclusion:

In this chapter, we've covered the basics of JavaScript program structure, including expressions, statements, variables, and the environment. However, this is just Part 1. Part 2 will explore essential concepts like console.log function, return values, and control flow. Stay tuned for deeper insights into JavaScript programming.