Basic Types and Variables

The Language of JavaScript

It's a Value-Based Proposition

Previously, we saw literals. In the statement:

console.log("Hello, World!");

"Hello, World!" is a string literal. It's a string because it's a sequence of characters surrounded by quotation marks. It's a literal because it always has that value.


Let's Not Be Literal

In contrast, we have variables. In javascript, a variable's type is undefined, and its value can vary (thus the name).

Instead of:

console.log("Hello, World!");

I could have written...

const message = "Hello, World!";
console.log(message);

Variables are variable

Variables are commonly declared by the let or const statement

let message;

To be of value, the variable needs to be initialized:

message = "I am Sam";

When we initialize a variable, it's type changes dynamically, based on the value it's given.


But what is the value?

Variables store a value, but allow us to change the value that is stored if we use the let keyword.

let message = "I am Sam";
console.log(message); // prints "I am Sam"

message = "I am Spartacus";
console.log(message); // prints "I am Spartacus"

But this doesn't work:

const name = "Sam";
name = "Spartacus"; // Throws an error

Reusing Variables

Notice that in the previous example, when we first used our message variable, we initialized it with the let statement:

let message = "I am Sam";
console.log(message);

When we reassigned its value, we didn't specify a type:

message = "I am Spartacus";
console.log(message);

That's because we had already declared the variable. We declare a variable only once.


Defining Variables

When we do this:

let message = "I am Sam";

We are really combining these two things:

let message;
message = "I am Sam";

Declaring vs Initializing a Variable

We declare a variable by using the let statement and giving a name. That's what we did here:

let message;

The variable's type is currently undefined. Its name is message.


Initializing a variable

When we initialize a variable, we are joining the declaration and assignment into a single line code.

let message; // Declaring message
message = "I am Sam"; // Assigning message

...can be simplified as...

let message = "I am Sam"; // Initializing message

Variable Naming Conventions

We will talk about naming conventions quite a bit. They're important, since they reduce the cognitive load of people that need to look at your code. (This person might be you.)

Apart from constants (more on that later), we use camelCase to name variables. The first letter should be lowercase, and the first letter of each additional word in a variable's name should be uppercase:

camelCase:

let myVariable = "my variable's value"; // Like this!

lowercase:

let myvariable = "my variable's value"; // Not this.

PascalCase:

let MyVariable = "my variable's value"; // Not this, either.

Basic Data Types

JavaScript is not a pure object-oriented language. That is to say that it allows for primitive types as well as objects. As JavaScript is a dynamic and weakly-typed language, we declare these primitives by assigning the correct values to them.


Booleans

A boolean variable holds either the value true or false.

const writingCodeIsFun = true;
const rootCanalsAreFun = false; // unless you're a dentist?

Number Primitives

The number type represents both integer and floating point numbers.

Integers are whole numbers and floating point numbers are numbers with decimal fractions.

const answerInt = 42;
const doubleValue = 42.16440330489723961032;

String primitives

With strings We denote their values with quotation marks:

let myDeclaration = "I can code";
myDeclaration = "But this works too";
myDeclaration = `So does this`;

Symbols

Symbols are a primitive value that always give you a unique value. They can only be created with the Symbol() constructor. There is no way to make a symbol literal. Due to their unique nature, they're great for id's.

const myNewSymbol = new Symbol()

The constructor also takes an optional description:

const myDescribedSymbol = new Symbol('some identifier')

undefined and null

These two are also technically primatives but should be avoided as much as possible.

undefined denotes that a value has not been defined. This is essentially the default value that JS gives things until you change that. You should (almost) always avoid actively assigning this as a value to anything.

null is similar to undefined with one important exception. null operates as a sort of quantified nothing. It means that there is nothing assigned to the value you're trying to access, but it's getting assigned nothing as opposed to simply not getting assigned.


Conclusion

These data types will be used in absolutely everything you code from here on out. So make sure that you are very familiar with all of them before moving forward.