Things I learned about variables on Codecademy
Tags: beginners, codenewbie, javascript, womenintech
Published: 2019-10-08 15:15:42

**More things I don't want to forget!:**

<li>Variables can be declared with let or const (used to only be var) let and const are preferred but var is still used, esp in older code.

<li>Camel casing means that words are grouped together. The first word is lowercase and every word after that has its first letter capitalised e.g. myName
= is the assignment operator

<li>You can say a variable has been ‘initialised’ with a value

<li>If you create a variable but don’t initialise it, it stores the primitive data type ‘undefined’

<li>Variable names can’t start with numbers. They’re case sensitive (don’t create two variables of the same name but with different casings — upper or lower etc) 


<li>They also can’t be the same as keywords — see here https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Lexical_grammar#Keywords  

<li>When initialising a variable with a value, a string has to be in ‘’ but numbers dont… i think. E.g.

```javascript
var favoriteFood = ‘pizza’;
console.log(favoriteFood);
var numOfSlices = 8;
console.log(numOfSlices);
```


<li>When assigning boolean values they don’t need ‘’

<li>The const variable stands for constant — it cannot be reassigned like let can. 
They also HAVE to be assigned a value when they’re written: they can’t be left undefined.

<li>Mathematical assignment operators += -= *= and /= perform the operation on the value in the variable with the number on the right, which adjusts the variable’s value. (When using these, stop putting an extra equals sign in front of them!) E.g.
```javascript
laterYears *=4;
```
<li>You can log the value of the variable with a string in front of it like this 
```javascript
console.log(‘The value of the variable is: ‘, levelUp);
//I think this is wrong but can't remember what it should be now, must check!
```
don’t forget the comma between the string and the variable name.

<li>Other mathematical assignment operators include the increment operator (++) and decrement operator (--). Placing these after the name of the variable either increase or decrease the value by 1. E.g. levelUp++;

<li>Use + to concatenate strings & variables e.g. 
```javascript
console.log(‘My name is: ‘ + myName + ‘.’)
```

<li>String interpolation (insert variables into strings) wrap the string with backticks and around the variable is ${variable} e.g.
```javascript
console.log (`My name is ${myName}. My favorite city is ${myCity}.`)
```
<li>Use typeof to check the data type contained in a variable e.g. 
```javascript
console.log(typeof newVariable); //returns string etc.
```

<li>Practice:

```javascript 
const myLove = ‘Laura’;
let yearsTogether = 2;
console.log(`I love ${myLove} and we have been together for ${yearsTogether} years.`)
console.log(typeof myLove);
console.log(typeof yearsTogether);
yearsTogether++;
console.log(yearsTogether);
console.log(`On November 27th we’ll have been together for ${yearsTogether} years. Woop.`)

/*This prints out the following
I love Laura and we have been together for 2 years.
string
number
3
On November 27th we'll have been together for 3 years. */
```
<li>Playing with the codecademy kelvin project:

<li>To round a value in a variable you use Math.floor() but to assign it to a variable (such as the one called farenheit) you would write:
farenheit = Math.floor(farenheit)
… I forgot to put the farenheit = first.

<li>Dog years project:

```javascript
const myName = ‘Laura’;
//variable to show my age.
const myAge = 25;
//variable for dog early years
let earlyYears = 2;
earlyYears *= 10.5;
//takes the already accounted for 2 years off the age
let laterYears = myAge — 2;
//multiply the age by 4
laterYears *= 4;
//adding together early and later years and putting them in a new variable
myAgeInDogYears = earlyYears + laterYears;
//prints out name, age and dog age.
console.log(`My name is ${myName}. I am ${myAge} years old in human years which is ${myAgeInDogYears} in dog years.`)
```