🎄 Day 16 - JavaScript basics
by Nino · 81 things on Twos
- ---
- Creating Variables
- JavaScript has variables
- like moving boxes, variables have content and names that tell us what's inside
- every variable needs a name
- variable names need to be single words and, therefore, have no spaces
- to help us understand what's inside a variable we pick descriptive names
- const windyCity
- variable names can contain numbers, too
- adding numbers is useful with multiple similar variables
- const car1
- we use the = sign to store a value inside it
- strings are words in double quotes
- const city = "Chicago";
- strings can contain all sorts of letters and symbols, including spaces
- "Winter is coming."
- we use const to declare variables whose values are not supposed to change. const is short for "constant"
- const age = 90;
age = 91;
// TypeError: Assignment to constant variable.
- let age = 90;
age = 91;
- to make it obvious that a variable is of type const and shouldn't change, we can optionally name it with uppercase letters
- const BIRTHDAY = "25/02/1882";
- const SPEED_OF_LIGHT = "3*10⁸ m/s";
- const MIDDLE_NAME = "Francesca";
- with the special instruction console.log(), we tell the computer to display a value in an area called the console
- console.log("Hello, world!");
- we can also use console.log() to display variables like greeting
- const greeting = "Hello, World!";
console.log(greeting);
// Hello, World!
- console is an area that displays output
- ---
- Using Variables
- we can update let variables as often as we want
- let currentStatus = "Watching Netflix";
currentStatus = "Relaxing at the beach";
console.log(currentStatus);
// Relaxing at the beach
- we can also give variables the values of other variables by setting one variable to the other
- let defaultStatus = "Hi there!";
let currentStatus = "Playing football";
currentStatus = defaultStatus;
console.log(currentStatus);
// Hi there!
- we call adding together string values an expression because it creates a single string value
- const followers = "55";
"Followers:" + followers;
// Followers:55
- const numberOfFollowers = "55";
console.log("Followers:" + numberOfFollowers);
// Followers:55
- const title = "Ms. ";
const name = "Irene";
console.log(title + name);
// Ms. Irene
- const numberOfLikes = "40 ";
console.log(numberOfLikes + "likes");
// 40 likes
- there are other kinds of values, too. Like numbers, which have no double quotes around them
- numbers make it easier to keep track of numeric values.
- const numberOfLikes = 5 + 1;
console.log(numberOfLikes);
// 6
- const private = 3;
const public = 10;
const total = private + public;
console.log("Total posts: " + total);
// Total posts: 13
- ---
- Using True and False
- there's a special value that's neither a string nor a number: true
there are no quotes around it, and it's not a numeric value
- true and false are great for situations like checking if a switch is on or if a feature is enabled.
- they show if a feature is switched on or off
- a ! sign in front of true makes the expression result in false
- if something is not true, it has to be false
- the ! sign is the negation operator. It turns values into their opposite
- console.log(!true);
// false
- const isMorning = true;
console.log(!isMorning);
// false
- const isMorning = true;
const isEvening = !isMorning;
console.log(isEvening);
// false
- ---
- Checking Number Equality
- we need to compare numbers in situations like checking a user's entered PIN matches their saved PIN
- to compare if two numbers are the same, we use the equality operator, ===
- when comparing, there are only two results: true or false
- console.log(10 === 10);
// true
- const votes = 10;
console.log(votes === 11);
// false
- to check if a number isn't equal to another number, we use the inequality operator, !==
- console.log(1 !== 0);
// true
- we can store the result of a comparison with the inequality operator in a variable
- const result = 1 !== 2;
console.log(result);
// true
- we can compare values with variables and variables with other variables
- const one = 1;
const two = 2;
console.log(one === two);
console.log(one !== two);
// false
true
- const petName = "Jessie";
const breed = "Labrador";
const age = 3;
const petDescription = petName + " - " + breed + ", " + age;
console.log(petDescription);
// Jessie - Labrador, 3
- const age = 3;
const isPuppy = age <= 1;
console.log(isPuppy);
// false
- ✓ equality and comparison operators, boolean value
<= and =>
- console.log("\n");
// special display statement, line break?
- ✓ variable concatenation
- .toFixed(2);
// formats numbers to two decimal places
// connect the value to this format using a period "."
- ---
- add learned declarations, etc.
- ---
- Mimo: semi-completed (15. JavaScript Basics)