Javascript course — Language basics, variables, data types

Marcel Mokos
ableneo Technology
Published in
3 min readNov 30, 2018

Part #2: Basics and Primitive types

Photo by Pierre Châtel-Innocenti on Unsplash

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.

read “Hello world” evaluated “Hello world” as a string and returns “Hello world” to the user

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
Boolean example (assert fail if the statement inside is not true).

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.

Number examples (assert fail if the statement inside is not true).
Number example comparing numerical values.

How to reuse values

We can write a statement that will declare a variable. Variables let us save values and reuse them multiple times.

declaring a variable called hello with value “Hello world” providing it to REPL output for the user will be “Hello world”
Life of a programmer is hard

let

  • The letis a statement to declare a variable
  • You can reassign the variable to a different value

const

  • Same as let but constcannot be reassigned
  • Once initialized you cannot change the value

var

  • 👎 Do not use! part of old specifications replaced by let
Asking the real question.

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:

One variable can hold different types during its lifetime

Declaring variables without initial values

The letstatement can declare a variable without an initial value. The variable value will beundefined.

declaring a variable without the initial value will output “undefined”

Reassign the variable to have a new value

You can reassign the variable to have a new value. The output will be “I❤️️Javascript”

“const” cannot be initialized undefined and cannot be changed

The execution will fail in both cases, our program will not run and Javascript REPL will print TypeError information

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.

Continue here

--

--

Marcel Mokos
ableneo Technology

I'm fanatic to next generation Javascript lambda, yield, async/await everything. I admire typescript and flow types. Javascript will ultimately rule the world.