Learning JavaScript, Day 3 – Variables and Data Types
I had to get up ridiculously early (shut up I'm unemployed 7am is ridiculous) for an appointment, so I'm tired, I'm cold (the weather app is lying to me, it is not 10°C/50°F, it is cold), but I'm just awake enough to curl up in my blanket, put on a Frutiger Aero playlist (after a brief moment for Kristopher Carter's "Delenn Love Theme" because apparently I also want to spend a bit of time being sad), and take on more JavaScript hell yeah.
JavaScript Variables
Oh so this tells me that var is an older way to declare a variable. Good to know. I do like the idea of using let, so I'll stick with that.
And then it gets into naming standards, which I get, reminds me not to give variables the same names as reserved keywords, sure, but then...
A convention among professional programmers is to start a name with underscore for "private" variables.
Private variables? Do I even want to know?
I also really like how they have a detailed list on what to use when.
- Always declare variables
- Always use
constif the value should not be changed - Always use
constif the type should not be changed (arrays and objects) - Only use
letif you cannot useconst - Never use
varif you can useletorconst
So my arrays will be in const but my individual weird variables might be let unless I never plan to change them, in which case they're const. I get that.
It then gets into what you can put into variables, and, again, I did not realise that I can include all my variables in one string.
So I can have:
let firstName = "Jeffrey",
lastname = "Sinclair",
rank = "Commander",
location = "Babylon 5";
(and let is good for that because, not to spoil a 30-year-old show or anything, but he might relocate...)
It's going to be hard to remember that == is "equal to", but I figure a few times messing it up will teach me to remember.
The rest is pretty straightforward. Do the arithmetic. Concatenate strings. That wacky thing where "5" + 2 + 3 results in 523 but 2 + 3 + "5" results in 55.
The exercises are getting a bit fancier now – no more just "which is these is correct", but actually typing in the variables and bits to make the code work.
Still nailing them though. Aw yeah, I'm going to get a good grade in JavaScript.
JavaScript Let
Ah, now we get into what "block" means and what scope is.
Blocks are things inside {} and if you declare a variable inside those curly brackets, you can't access it outside of it.
So I were to be like:
let commanderName = "Jeffrey Sinclair";
{
let commanderName = "John Sheridan";
}
window.alert("The commander of Babylon 5 is " + commanderName)
It returns "The commander of Babylon 5 is Jeffrey Sinclair". Which, y'know, always and forever, oh commander my commander.
It also explains what the difference between let and var is, aside from the fact that one is newer. var can be accessed outside of blocks, which means that if I changed the above to be var commanderName, it'd tell me that the commander of Babylon 5 is John Sheridan, and we can't have that.
But anything inside a function can't be called outside it, no matter how you declare it. So if I had:
let commanderName = "Jeffrey Sinclair";
function babylon5() {
var commanderName = "John Sheridan";
}
window.alert("The commander of Babylon 5 is " + commanderName)
That's not going to change my commander, no matter that there's a var in there.
I also can't redeclare let variables, so I can't have:
let commanderName = "Jeffrey Sinclair";
let commanderName = "John Sheridan";
It doesn't work that way. It can work that way with var, but why would I want to do that?
The course then tells me that although I can't redeclare let variables, I can "reassign" them. I'm not sure what that means, but I'll find out.
Oh, but then it goes "Hey, just so you know, let and const aren't supported by anything lower than Internet Explorer 11 and a lot of browsers before 2017, so, well, sorry for your ancient programs!"
To be fair, the only ancient browser I'm going to use is Lynx, and it's not like that supports any JavaScript, so...whatever. It is good to know what might happen if I have to support a bunch of old machines, though. Lord knows they're hiding out there. Waiting.
It then tells me about var "hoisting", where I can apparently use the variable before I name the variable? This is so weird what the heck.
Why would I do this?
commanderName = "Jeffrey Sinclair";
var commanderName
I mean, it just seems...awkward.
let doesn't do that, so that's nice.
JavaScript Const
Since we've done let, it's time to do const. You can't change what a const is in your script, which is definitely nice.
So, like, if I were to do my weather converter:
let cTemp = 13;
const fTemp = (cTemp * 1.8) + 32;
Because cTemp would vary, but fTemp is always going to be the cTemp and some math.
The course does point out that while you can't reassign things that use const, you can change things inside a const, like if it was an array.
It also gets a bit into objects, which I'm not 100% sure on, but I also know that's down the line, so I just have to remember that const objects can have properties change in them.
JavaScript Datatypes
I debated doing this one today, because really, I've done a lot, but this finishes the section on JavaScript Syntax, so, y'know, what the heck.
This is about the data types that can be in JavaScript variables.
- Strings: Characters enclosed in quotes (
"Jeffrey Sinclair") - Numbers: Mathematical values (
5) - Bigint: Numbers representing large integers (
BigInt(1234567890123456789012345)) - Boolean: True or False data (
trueorfalse) - Object: Key-value pairs of data (
firstName:"John", lastName:"Sheridan") - Undefined: No assigned values (
x) - Null: Nothing is here (
x = null;) - Symbol: Unique identifiers (
x = Symbol();)
If you're working with big numbers, you can use bigint. I love that it's called "bigint". Yes, that is a big integer. Good for you!
I can apparently use scientific notation when working with big numbers, but I'd have to remember scientific notation, and, yeah, no, that has escaped me.
Boolean is boolean is boolean, use the ==, !=, < and > to compare two things and have it tell you what's true and what's false.
Undefined actually comes in handy, because that's stuff that will be defined later, either by calculation or user input.
So, like, when I learn how to get user input for my weather converter, it'd be:
let cTemp;
const fTemp = (cTemp * 1.8) + 32;
Because cTemp would be entered by the user.
So after going through all the data types, I do the exercise and oh no! I get my first wrong answer!
If a variable is declared using the
letkeyword, it can never change its data type.
The correct answer is "False". I spaced out a bit there, but now I know.
Day 3 — Results
varis an old way to declare variables,letandconstare more modern.constis for variables that won't change or types (like arrays).letis for variables that will change.- You can start variable names with an underscore. You can also have numbers, underscores, and dollar signs in them if you want.
- You can put your variables on one line (or in one string if you want to break them up with line breaks) by using commas.
=is "this thing is that thing".==is "equal to". They are different things.- If the first number is a string, the rest of the numbers will be seen as strings.
- Variables declared with
letcan't be called outside of blocks (anything in{}). - Variables declared with
varcan be called outside of blocks. - But anything inside a
functioncan't be called outside the function. - You also can't redeclare
letvariables, but you can withvar. letandconstdon't work in older browsers (usually pre-2017)- You can change the properties inside a
constarray or object, but you can't change theconstitself. - There are eight data types in JavaScript. Strings, numbers, bigint, boolean, object, undefined, null, and symbol.
- You can use scientific notation with bigint.
- Undefined is used when the value of the variable will come in via user input or calculation.
- Undefined isn't an empty value. Empty strings exist.
I really like that I'm learning how JavaScript works, rather than just making it do things. I mean, I like getting stuck in and making things do things, that's why I keep on changing it from the pre-written exercises into weird things, but I'm definitely picking up more by understanding what's happening.
Tomorrow, I have to go get a retina scan, so instead of learning JavaScript, I'm going to be a lady of leisure with giant sunglasses. Maybe I should wear my Lauren of Palm Springs caftan and one of the turbans I picked up during my chemo round, and really bury myself in the part.
"Why, Officer! I was getting my eyes tested! I don't know who could have possibly murdered that man!"
Today's Sticker

A very shiny Pride Mothman from Eldritch Rach. Because Mothman believes in love.