“Getting to know JavaScript” memo

Commands

Every program is a set of commands. In JavaScript, the commands are separated by a semicolon, ;

The program is executed sequentially, from top to bottom, command after command.

The code inside the comments is not executed. Examples of comments:

// This line of code will not be executed. Single-line comment.

/*
These lines of code will not be executed.
This is a multi-line comment.
*/

The console.log() command can be used anywhere in the program. It can return numbers, strings (they must be expressed in quotes), results of some operations.

Variables

A variable is a name for data that is understandable to people.

You can create a variable with the var command followed by the name of the variable. You can write the name using the camelCase style:

var myNumber;
var userName;

or snake_case:

var my_number;
var my_name;

Variable names in JavaScript are case sensitive: myname and myName are two different variables.

The variable name must begin with a Latin letter and can only contain Latin letters and numbers.

You cannot use special keywords such as var or if as the name of the variable. Here is the Full List of these keywords.

The var command is used only once to create each variable. We then access the variable by its name, without var.

To assign a value to the variable, use the equal sign =.

After the variable is created, it can be used in other commands, for example, it can be logged in the console.

If a new value is assigned to the declared variable with a value, it will override the old value. This is called overriding the value of a variable.

var milkInGrams = 20;
console.log(milkInGrams);
// Logs 20

milkInGrams = 100;
console.log(milkInGrams);
// Logs 100

Operations

Examples of operations: addition (+), subtraction (-), multiplication (*), division (/).

Variables can be part of the operations:

milkInGrams * 0.5;

You can use parentheses to change the priority of operations

var firstNumber = 100 + 50 / 2;
var secondNumber = (100 + 50) / 2;

If a string is involved in the addition operation, the result will be presented as a string:

console.log('Milk, g: ' + 50);
// Logs 'Milk, g: 50'

Addition of strings is concatenation.


Continue

We are using cookies to gather information which will help you use our website most effectively. You can read about this here or disable this feature if you want. By continuing to browse the site, you agree to our use of cookies.