So we've got a little hello world program, and there's not much in it. It's time to learn about variables and their types! Take this thing up a notch!
Variables are named pointers to some information in your computer's memory. That's basically the whole thing.
You declare variables by giving them a name, then at some point you give them a value. Often you'll do that all in one go, just like this: let x = 1;
This declares a variable called x
which can now be used in other parts of the program you're using. Right now, it's value is the number 1. Super cool.
Let's add some of this to your program.
const delay = require('delay').default;
async function main() {
console.log("Hello");
// await delay(2000);
console.log("world!");
let x = 1;
console.log(x);
x = 10;
console.log(x);
}
main();
Notice these additions. First, I commented out the delay line. That's done with two forward slashes //
. That basically tells node to ignore anything on that line following the two slashes.
Next I'm declaring a variable, printing it out, then changing that variable, and printing it again.
const
vs let
.
Sometimes, you'll have variables that you need to make sure won't change. Like our variable that points to the delay package. You do that by declaring the variable as a constant value using const
(this is also referred to as immutable). When you have a variable you want to be able to change, use let
(aka, mutable).
When node runs, if you accidentally try to change a constant variable, it will yell at you. This is a good thing (I mean, usually. Unless you break your entire program. I've never done that......)!
Ok, so variables all have types. Types are important, because node will give you dope little things you can do for specific types. Each type of variable takes up some amount of space on your computer's memory. Some types, more than others. This is a huge concern on machines that have small amounts of memory, but on modern computers, you're probably good to go.
I'm going to have us change up our app a little bit to give some examples...
async function main() {
let helloWorldVariable = "Hello world!";
let x = 10;
let trueOrFalse = true;
let whatTheHeckIsThis;
console.log(`${helloWorldVariable} is a ${typeof helloWorldVariable}`);
console.log(`${x} is a ${typeof x}`);
console.log(`${trueOrFalse} is a ${typeof trueOrFalse}`);
console.log(`What the heck is this is ${typeof whatTheHeckIsThis}`);
}
main();
typeof
comes from node, and let's you check what type of variable you're working on. It returns a string with the name of the type. Running this will give you an idea of what I mean.
Cool thing to note here, I'm using a string builder. My helloWorldVariable
is simply a string, but in each of my console.log
statements, I'm building a string with variables in line. That's the tilde symbol, then ${}
around any variables I want to show.
Each variable type does cool stuff. Here's the important ones to worry about.
- Number
- Pretty straight forward.
- Integer, decimals
- String
- This is a series of characters
- I say characters, because it's an important distinction. Strings are actually arrays
- Arrays
- A collection of individual items
- BURN INTO YOUR MIND Arrays start at 0!!!!
- Booleans
- True or false
- Object
- Really cool things! I'll have an entire post about those bad boys!
Let's try mixing and matching some of these beauties in our app.
async function main() {
let helloWorldVariable = "Hello world!";
let x = 10;
let trueOrFalse = true;
let whatTheHeckIsThis;
console.log(`Is trueOrFalse set to false? ${trueOrFalse === false}`);
console.log(`${x} + 1 = ${x + 1}`);
console.log(`Uh, did I change the value of x? x is ${x}`);
console.log(helloWorldVariable.repeat(x));
console.log(`Is whatTheHeckIsThis a number? ${typeof whatTheHeckIsThis === "number"}`);
whatTheHeckIsThis = 0; // look! it's defined now!
console.log(`Is whatTheHeckIsThis a number? ${typeof whatTheHeckIsThis === "number"}`);
}
main();
I introduce the equivalence operator ===
. It checks if two things are strictly the same. Remember that typeof
returns a string, so I'm checking if the string is equal to what I want it to be.
We're using some of the built in methods that node has for types. Which is pretty cool. When using vs code, suggestions pop up as you type. That's handy. My favorite is the repeat method that comes with strings. You'll for sure never ever use it, but it's still fun.
Arrays! Super important little dudes.
Remember this idea, an array is a collection of things. They can be all sorts of things. They can be variables you've already declared, or just inline variables that you just jam into an array too. The have []
around their items. Check out the code here.
const delay = require('delay').default;
async function main() {
let helloWorldVariable = "Hello world!";
let x = 10;
let trueOrFalse = true;
let whatTheHeckIsThis;
let arr = [helloWorldVariable, x];
console.log(arr);
// .push() is a method on all arrays that you can use to new items to an existing, mutable array
arr.push(trueOrFalse);
arr.push("Here's a string I added without having declared a variable name for it yet!");
arr.push(whatTheHeckIsThis);
console.log(arr);
console.log(`How long is the array? ${arr.length}`);
let newString = arr[3];
console.log(newString);
whatTheHeckIsThis = "I defined it now as a string! Did it change in the array???";
console.log(arr);
}
main();
Lots of cool things happening here!!! We've got our variables up top, and I've added a new variable that's an array called arr
. Quick print out, then we add some more items to the array. I then make a new variable and assign it the value of the 4th item in the array. I go back and define my undefined variable, but this doesn't change what I've already tossed into the array. That item in the array is still undefined.
I just know you're thinking something like, "What the heck? Why would we use arr[3]
to get the 4th item in an array? Moreover, why the heck does that even work in the first place????" Well each item you toss into the array gets a pointer to it called its index. When you call for an item in an array by its index, you get back the value. BUT!!! Indexes start at 0. Don't ever forget that or you will have a super bad time. It gets painful because the length of the array is 5, but the last index is 4. Sleep on it, but don't forget it!
Next big thing we'll get into is Objects! But that's its own post.