Variables to store values 👜
// declare a variable
// initialize the variable
bestColor = "darkcyan";
// declare and initialize at the same time
var prettyColor = "cadetblue";
it is recommended to always declare variables at the top of their scope (the top of global code and the top of function code) so it's clear which variables are function scoped (local) and which are resolved on the scope chain.
- Safe variable naming convention: lowerCamelCase
- Numbers and underscore but not first
- Case sensitive
- …and remember no JS reserved words!
- Numbers, strings, booleans, arrays, and objects
- JS is a dynamically typed language because I don’t need to specify a data type for variables
typeofis an operator I can use to return the data type of the variable I pass into it
Math with numbers and operators 🔢
I noticed at the variables section that I should stop crawling, and try to speed up! Same here, after Learn Python The Hard Way, I know what integers, floats and number systems are now.
Console practice session gave me a syntax error when decaring and initializing a variable with a float, so I need to remember that even though the decimal separator in Norwegian is a comma
3,14 that will be interpreted as an array missing square brackets. 🤪
var thisNumber = 666;
// returns "number"
var thatNumber = 3.14;
// returns "number"
This section also covers basic arithmetic operators and operator precedence, how to use parantheses to calculate parts of an expression first and so on. Yup, all good.
Increment and decrement operators
var start = 50;
// returns 50
// returns 51
If I use the incrementer after the variable like that, the browser will first return the current value — then increment. If I use the operator first like
--myVariable; the browser will return the incremented/decremented value.
Ha, I see the internet is full of people asking why Python doesn’t have a
++ operator. Well, I learnt Python style increment by
+= first, so I think that makes way more sense.
This is where I reach the part introducing JS assigmenent operators that… work exactly the same way?! Right.
Looks all familiar, also noting that they here recommend using strict
!== to test both value and datatype.
Not much new in this section, but console exercises are good practice for me. These MDN tutorials get you to type a lot in the console. Also stuff that doesn’t work, so when you do get errors later, you’ve seen the syntax errors before.
- Escape notation
- Concatenate is joining together
- String literal
When I want to convert a string to a number, I can pass the string into the
number object and it will return a string if it can.
If I need the opposite:
toString(). This is a method to the number object. I don’t pass the number into it like the previous example because…?! Not sure, but it will probably become more clear soon.
Built-in methods to do stuff to strings 🎶
Module: Useful string methods
var chocolateCake = "That variable will become a string object instance";
- How long is that string? Use the
- Retrieve a specific character? Use square bracket notation
- Need to grab a substring? Use this method
- Want to extract it? Use the
slice()method with character positions.
- Change words? Sure! Use the
chocolateCake = chocolateCake.replace("string object instance", "cake");
✅ Finished the active learning examples. Had to peak just a tiny bit for hints in the solutions for the first two, but flew through the last one. Yay!
Make lists of multiple data items 🌈
var beer = ["stout", "brown ale", ["ipa", "neipa", "black ipa"]];
// modify an item with bracket notation
beer = "porter";
// beer now returns [ "stout", "porter", (3) […] ]
// returns "ipa" within the multidimentional array
Find length of an array
// returns the number of IPAs as 3
beer[beer.length - 1];
// returns the last item of the IPAs: "black ipa"
…and more commonly,
length is used to tell a loop to keep going through all items of an array.
Split data in a string to an array
var ourRawData = "Oslo,Bergen,Trondheim,Stavanger,Tromsø";
var ourAwesomeArray = ourRawData.split(",");
// returns [ "Oslo", "Bergen", "Trondheim", "Stavanger", "Tromsø" ]
var lastCity = ourAwesomeArray[ourAwesomeArray.length - 1];
lastCity; // returns "Tromsø"
Join items in an array to a string
var myNewString = ourAwesomeArray.join("/");
myNewString; //returns "Oslo/Bergen/Trondheim/Stavanger/Tromsø"
Add or remove array items
pop()at the end of the array
shift()items at the beginning
The new length of the array is returned when the method call completes
…which means if I need it, I can store it in a variable. Same with an item removed from the array, I can use a variable to save it. Here I will remove the first city from the list:
var currentCity = ourAwesomeArray.shift();
// returns [ "Bergen", "Trondheim", "Stavanger", "Tromsø" ]
currentCity; // returns "Oslo"