JavaScript basics
In this lesson, we examine some basic JavaScript concepts such as how to add JavaScript expressions, semicolons, and more.
A JavaScript program is a list of instructions that you give to the computer for execution.
If you tell the computer “add two plus four and print the result” the computer will calculate 2 + 4 and print 6.
The browser’s interpreter is in charge of “executing” your JavaScript code. Web browsers have what is called a JavaScript interpreter.
This interpreter will read your JavaScript statements and then interpret and execute them one line at a time.
Semicolons
Use the semicolon (;) to indicate to the JavaScript interpreter that you finished writing the current code statement.
It’s an instruction telling the interpreter about the end of the current statement and the beginning of a new one.
var num1 = 10; var num2 = 20; var result = num1 + num2; console.log(result);
JavaScript statements and values
A JavaScript statement is a code that contains Values, Expressions, Keywords, Operators, and Comments
JavaScript has two types of values.
1. Fixed values
The first type is fixed values, also called literals. Because they are fixed values, they cannot change.
Literals can be of any of the following types:
- Array,
- Boolean,
- Integers,
- String, etc.
Literals Example
// Literals "5" + "6" + "7"; // String literal 5 + 6 + 7; // Integer literal
Array literals Example
// Array literal var carModels = ["BMW", "Mercedes", "Audi"];
2. Variable value
The second type of value found in JavaScript is a variable value, also called variables. Variables can change, but literal values remain fixed and cannot change.
A JavaScript variable is declared with the keyword var, and the equals sign is used to assign a value to a variable.
You can cover JavaScript variables later on in this JavaScript tutorial.
Variables Example
// Variables var myVariable = 15; myVariable = 20; console.log(myVariable);