Variables, Values, Types, and Operators
In today’s lesson, we’ll learn how programs store data and how to manipulate data.
A program needs to “remember” values. For instance, if you sign up for a newsletter, the program needs to retain your email address. To simplify things, let’s pretend it looks something like this:
var emailAddress = “firstname.lastname@example.org”;
We declare a variable above with the var keyword. We then name it something, which could be anything, but by convention, they’re typically named in a way which makes them easy to understand and read. In my case, I used “emailAddress.” You’ll see variables usually being declared in “camelCase,” such as “emailAddress.” Basically, the first letter is lowercase and the words that follow are uppercase.
People have different preferences, and you can choose whichever one you like more, but it’d be wise to stick to it! Next, we have an assignment operator, which is just an equal sign (=): the variable emailAddress is equal to;. It’s essentially the same thing we learned in school, only now we can have different values. To the right of the operator, we have the value. In our case, the value is of type string.
Strings are identified by quotation marks, “”. They can be double, like above, or single quotes: ‘email@example.com’.
The semicolon is similar to a period in English; that line of code is finished, so move on to the next.
Aside from the type “string,” there are three more:
• boolean (true or false values)
Numbers. Numbers don’t need much explanation; we’re all familiar with them.
Let’s create a variable with a number value:
var five = 5;
console.log(five); // 5
Note: When you see “//,” it’s the beginning of a “comment.” It’s not a part of the program, but simply a message for humans—i.e., you! I’ll use them mainly to show the output of the code.
That’s all there is to numbers!
Boolean values. Boolean values are either true or false and used within programs to decide a course of action to take. If a condition is true, we may tell our program to do something; if it’s false, we can do something else.
var truthy = true;
var falsy = false;
Undefined. Undefined is a value that is unassigned and just means we haven’t declared a value to our variable. This could be useful for our program, which awaits a response from a user.
console.log(x); // undefined(no value)
We’ve already been familiarized with two operators above: the assignment operator, =, and the typeof operator. There’s no need for us to go through all the operators, as that would fill an entire lesson on its own. We will go over those that are elegant and simple enough for anyone to understand.
5 + 5; // 10
10 – 5; // 5
25 / 5; // 5
5 * 5; // 25
// Incrementation (more on this in Lesson 5)
Comparison operators. We can also compare numbers with comparison operators. You might remember these from elementary school, where we learned that the alligator eats the bigger number.
10 > 5; // true
5 < 10; // true
Simple enough. And there’s more!
// Less than or equal to:
5 <= 10; // true
// Greater than or equal to:
5 >= 10; // false
// Equal to:
5 === 10; // false
Likewise, we can compare strings:
“apple” === “apple”; // true
Logical operators. Logical operators are similar to comparison operators, they return true or false.
// Logical OR || that reads: 5 is equal to 10, or 5 is less than ten:
5 === 10 || 5 < 10; // true
// Logical AND && that reads: 5 is less than 10, and 5 is greater than 1:
5 < 10 && 5 > 1; // true
// Logical NOT ! that reads 5 is not equal to 10:
5 !== 10; // true
Tomorrow will consist of essential building blocks of programming: if, else, and else if. Stay tuned!
Share with friends