At a scale, computer programming consists of two core concepts. These are Algorithms and Data Structures.

Let’s start with the data. To make the information easily digestible, we won’t get hung up on terminology, but get straight to the point. Today we will break down 3 simple JavaScript data types:

  1. Number
  2. String
  3. Boolean

There are no surprises with numbers in JS. 1, 2, 1000, 0.99, 15.55 are all numbers. You went through this in elementary school and everything is stable here.

String - is pretty much any sequence of text characters, such as 'Hello, World' Or 'JavaScript rules!. All strings should be wrapped in quotes.

IMPORTANT: If you wrap a number in quotes, it will no longer be a number, but a string. For example: 10 is a number, while '10' is a string.

The boolean type is not so obvious if you’re new to programming. It was invented specifically to describe situations in which only 2 meanings are possible - true and false. Accordingly, the set of valid values for a boolean type is limited by the values true and false.

Creating variables and storing data

For data to be usable, it needs to be stored somewhere. Most commonly this “somewhere” is called a variable.

You can create a variable using the let keyword, and store the value into it using the = operator. For instance:

let greeting = 'Hello, world!';
let salary = 100000;
let isWinning = true;

The variable name must be specified immediately after the let keyword.

As the name suggests, a variable can change its value. To change the value of a variable, we need the same = assignment operator.

let greeting = 'Hello, world!';
greeting = 'Hi';
console.log(greeting);

In this example code above, several things happen:

  1. In the first line, we create a greeting variable and immediately give it the value Hello, world.
  2. On the second line, we change the value stored in greeting to Hi.
  3. In the third line, we print the value of the greeting variable to the screen. And what will it be?

Hi?

— Exactly! We’ve updated the value of greeting, and then printed it out to the screen.

Looks similar to signing a container or a box in a warehouse.

— On the one hand, yes. A variable is a box, you can put something useful in it. And the name is the inscription on the box, by which it can be easily found. But it would be more correct to say that the value is one box, and the variable is another.

The variable does not store the value itself, but only the address of the box with the value. This is very convenient because allows multiple variables to point to the same value.

let salary = 100;
let oldSalary = salary;

// both variables point to the same value
console.log(salary);            // 100
console.log(oldSalary);         // 100

salary = 200000;

// salary is 200000 and oldSalary is 100!
console.log(salary);            // 200000
console.log(oldSalary);         // 100

Here’s what’s going on in the code snippet above:

  1. Create a variable salary and bind it (store the address) with the value 100.
  2. Create a variable oldSalary and bind it with the same value 100.
  3. Display the values for salary and oldSalary to make sure they are equal.
  4. Bind the variable salary to the new value - 200000.
  5. Display the values of the salary and oldSalary variables again to ensure that only salary has changed!

Indeed, if it had stored the value, and not the address of the value, it would not have been possible to do this. oldSalary would have changed at the same time when salary has changed.

— Ha! A bit more practice, and you’ll be a good sub for me, Hero.

— Professor, can we create empty variables or store “nothing” in them? The box may well be empty.

— Sure! In JavaScript, “empty boxes” contain the value null or undefined. There are some differences between them, but you shouldn’t worry about them yet.

There are 2 ways to get an empty box variable:

  1. Create a variable, but do not assign a value to it: let salary;
  2. Explicitly set the value of a variable to undefined or null using the assignment operator: salary = undefined.

By the way, we rarely use both of these methods in practice, and more often one value is replaced by another, without intermediate “release”.

Formatting variable names and camelCase

Remember that greeting, Greeting and GREETING are 3 different variables. I think you already guessed why.

— Yes, uppercase and lowercase letters seem to matter.

— Right, the uppercase A and the lowercase a are different characters and for a computer, the difference between them is about the same as between the numbers 65 and 97.

— Why exactly these numbers, and not 1 and 2, for example?

— It’s too early for us to talk about this, but if you really want to, you can explore the topic of ASCII Character Set whenever you feel like it.

If you want to use multiple words to name a variable, there are several ways to do it. JavaScript programmers use camelCase in most cases.

The rules are simple:

  1. All the words are written together.
  2. The first word starts with a lowercase letter.
  3. All the rest start with an uppercase one.

For example, if you want to create a variable and store the home address there, then in camelCase it will be homeAddress. There is no word limit, the variables userNotificationPreference, dailyMorningRoutine, hasUpdatedSubscriptionSettings are perfectly fine.

— Looks a little strange, but it seems clear.

— JavaScript, by itself, does not prevent you from creating variables home_address or HomE_AddresS. It’s just that in most projects programmers use camelCase rules.

It’s better to be on the same tune with colleagues than trying to reinvent the wheel.

Another unspoken rule concerns the naming of boolean (logical) variables. Try to start the names of such variables with the words is or has. For example, if you want to mark some visual element as “clickable”, then this can be stored in the variable isClickable, instead of just clickable.

— Quite convenient, you can immediately understand that we are dealing with a boolean variable, and not a string or number.

Constants

— Some variables never change after they have been initialized. The number of hours in a day, days in a week, seasons, and many other values remain constant, no matter what happens in the program.

On the one hand, you can make use of the let keyword and declare them as regular variables. But, then we will not be protected from accidentally changing these values later in the code.

Is that a problem? I will just remember that some variables do not need to be touched and will not change their value.

— You will, of course, remember. But:

  1. Sooner or later you will forget which variables can be changed and which cannot.
  2. Other programmers can’t read your mind.

And how are we going to fix this?

— Use the keyword const instead of let.

const numberOfSeasons = 4;

If someone tries to change the value of the constant numberOfSeasons - an error will occur.

— It is useful, so we will immediately understand where the problem is and will not need to figure out where the spring and summer have gone.

— Exactly!