Javascript: expressions and variables
By Dmitry Kabanov
I was recently studying Javascript out of curiousity as this language is used so much in the current web and mobile development. These are notes on basic Javascript knowledge to better memorize its syntax.
Main sources for learning that I use are fantastic book Eloquent Javascript and this interesting course on Domestika.
Expressions
Javascript is a C-family language, so basic arithmetic operations are exactly the same as in C:
3 + 8; // Sum, 11
2 * 5; // Product, 10
7 % 3; // Remainder, 1
as well as logical operations:
5 > 2; // true
7 < 5; // false
9 <= 10; // true
20 >= 20; // true
Equality can be checked with ==
operator but this operator thinks that zero is
equal to false
(that is, it does type casting), while special operator ===
does not do this, it does not change the types.
It is recommended to use the latter operator most of the time.
Variables and constants
Variables can be defined with the let
keyword. Pre-2015 Javascript used
the word var
, which can also be used still but let
is recommended.
let a = 25;
In the above code, the variable a
is declared and initialized to value 25
.
One can also declare a variable without initializing it:
let b;
then it has special type undefined
.
Constants (that is, variables that cannot change their values) are defined
using keyword const
:
const MEANING_OF_LIFE = 42;
It is customary to use upper case letters and underscores for constants.