Commenting & Variables
What is a comment?
A comment is a way to leave notes for anyone who reads the code.
In JavaScript, the pattern //
is reserved to create a single line of code that will be converted to a comment.
The JavaScript compiler will always ignore a line of comment when it sees the pattern of //
in the beginning of the line.
How does a JavaScript program execute its code?
The JavaScript compiler which translate our written instruction to a language that a computer can understand will translate the JavaScript code in order from top to bottom.
The code will then execute its instruction from top to bottom.
What is a variable?
Variables are named placeholders that allow a programmer to attach important data that a program would need to an easy label that can be used to reference later.
Variable Naming Convention
start with lowercase letters
labels that have multiple words are either
camelCased
or useunder_scores
labels cannot be the same as the built-in keywords in Java
labels should not be ambiguous
let a = "Park";
is less descriptive thanlet last_name = "Park";
Our Program Above
Comment:
This is a single-line comment. Comments are used to leave notes or explanations in your code. They are ignored by the JavaScript engine and do not affect the execution of the program.
Variable Declaration and Initialization:
let
is a keyword used to declare a variable. In this case, the variable is namedgreeting
.The variable
greeting
is assigned the string value"Hi, how are you?"
.
Output to Console:
console.log()
is a function that prints the value ofgreeting
to the console.When this line is executed, it will output:
Hi, how are you?
So, when you run this program, it will display the message "Hi, how are you?"
in the console.
Last updated