Javascript course — Language basics, variables, data types
Part #2: Basics and Primitive types
Confusing parts for beginners.
;
semicolons are optional but we will use them. Semicolons are end statements on its own line.""
quotes used to represent text. Always used as pars. Optionally quotes can be written by',",`
. By default use"
double quotes (one character).//
for one line and/* */
for multi-line comments behave like whitespace and are discarded during script execution.{}
curly brackets are used for enclosing a block of code, or for declaring Object literals.()
parenthesis in JavaScript is used for function calls, to surround conditional statements, or for grouping to enforce Order of Operations.
Work with Text
The "Hello world"
was used in console and REPL example. You have to tell the program what should be interpreted as text using quotes. The type used in javascript for text is called string.
Template literals (Template strings)
An expression can be a different string. Later we will use variables or even function inside.
`string text ${expression} string text`
Work with logic
Boolean represents a logical entity and can have two valuetrue
, andfalse
.
- for strict equality use
===
- for strict not equality use
!==
- always use the strict form with triple equals since it will check if you are using the same value type
There many more Logical operators we can use but for now, we will keep it simple.
Work with numbers
Numbers are written without quotes and you can perform basic arithmetic operations with them.
How to reuse values
We can write a statement that will declare a variable. Variables let us save values and reuse them multiple times.
let
- The
let
is a statement to declare a variable - You can reassign the variable to a different value
const
- Same as
let
butconst
cannot be reassigned - Once initialized you cannot change the value
var
- 👎 Do not use! part of old specifications replaced by
let
Dynamic typing
JavaScript is a loosely typed or a dynamic language. Variables in JavaScript are not directly associated with any particular value type, and any variable can be assigned (and re-assigned) values of all types:
Declaring variables without initial values
The let
statement can declare a variable without an initial value. The variable value will beundefined
.
Reassign the variable to have a new value
“const” cannot be initialized undefined and cannot be changed
Summarization with theory
Data types
Text in javascript is represented by the String type. The Number and Logical values use Number and Boolean types. The summary of data types is almost complete when we add already covered Undefined type and other that we will explain in detail later.
The latest ECMAScript standard defines seven data types:
and Object
What is primitive
Most of the time, a primitive value is represented directly at the lowest level of the language implementation.
A primitive (primitive value, primitive data type) is data that is not an object and has no methods. In JavaScript, there are 6 primitive data types: string, number, boolean, null, undefined, symbol (new in ECMAScript 2015).
Let me explain what Function is before we focus on Object and Null later.