Programs, Scripts, and console.log()

Programs and scripts

A program is a collection of program statements that performs a specific task when a computer runs it. A program is often called software.
A program can be written in many programming languages. We will use JavaScript. By convention JavaScript code is stored in a file called a script, whose name ends in .js, and you write scripts in an IDE (Integrated Development Environment). Each statement written on its own line ends with a semicolon.

Your first program

The console.log() function displays a message on the console, the panel where a running program shows its output. Characters enclosed in quotes, single or double, form a literal string. The quotes mark where the text starts and ends, so they are not part of what gets printed.

Every console.log() ends the line

console.log() finishes its output with a newline character, a special control character that marks the end of a line. Call it with nothing inside and it has nothing to display, but it still ends the line, so you get an empty one. Inside a string you can also write a newline yourself, as a backslash followed by the letter n.
CodePrints
console.log("go");go
console.log();an empty line
console.log("east\nwest");east, then west on the next line

console.log() is not only for text

A calculation written inside the parentheses is worked out first, and the result is what reaches the console. Numbers print without quotes, because they were never text to begin with.
CodePrints
console.log(45);45
console.log(2.75);2.75
console.log(31 + 60);91

Several values at once

console.log() accepts any number of arguments separated by commas: zero, one, or many. It prints them in order and puts exactly ONE space between each neighbouring pair. That is how you mix text with numbers and calculations on a single line.
If you would rather control the spacing yourself, glue the pieces into one string with a plus sign and log that instead. A plus sign contributes no space of its own, so console.log("pop" + "corn"); prints popcorn with nothing between the halves.
The program below prints five lines for a library book sale. Run it, then change it and run it again:
• Put quotes around 29 + 39, so that the line reads console.log("Paperbacks sold:", "29 + 39");.
• Delete the line console.log();.
• Change the comma in console.log("See you", "next week"); to a plus sign.
console.log("Library book sale");
console.log();
console.log("Paperbacks sold:", 29 + 39);
console.log("Each one costs", 2, "dollars");
console.log("See you", "next week");
Output
Press Run to see what this program prints.

Note: This IDE is for experimenting with the code in this lesson. Your changes are not saved, and refreshing the page puts back the original lesson code.