DEV Community

Helen Kent
Helen Kent

Posted on

Things I learned about variables on Codecademy

More things I don't want to forget!:

  • 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.
  • 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
  • You can say a variable has been ‘initialised’ with a value
  • If you create a variable but don’t initialise it, it stores the primitive data type ‘undefined’
  • 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)
  • They also can’t be the same as keywords — see here https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Lexical_grammar#Keywords
  • When initialising a variable with a value, a string has to be in ‘’ but numbers dont… i think. E.g.
    var favoriteFood = pizza;
    console.log(favoriteFood);
    var numOfSlices = 8;
    console.log(numOfSlices);
    
  • When assigning boolean values they don’t need ‘’
  • 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.
  • 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.
    laterYears *=4;
    
  • You can log the value of the variable with a string in front of it like this
    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.

  • 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++;
  • Use + to concatenate strings & variables e.g.
    console.log(My name is:  + myName + .)
    
  • String interpolation (insert variables into strings) wrap the string with backticks and around the variable is ${variable} e.g.
    console.log (`My name is ${myName}. My favorite city is ${myCity}.`)
    
  • Use typeof to check the data type contained in a variable e.g.
    console.log(typeof newVariable); //returns string etc.
    
  • Practice:
    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. */
    
  • Playing with the codecademy kelvin project:
  • 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.
  • Dog years project:
    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.`)
    
  • Top comments (4)

    Collapse
     
    georgecoldham profile image
    George

    Awesome start, whats next on the learning list?

    Wish I had though to document my learnings when starting out!

    Collapse
     
    helen8297 profile image
    Helen Kent

    Thank you! Well I'm making my way through HTML/CSS and JavaScript on codecademy. Also went to a meetup last week and learned a bit about git/github - would like to carry on with that. I'm going to apply for a bootcamp soon so really hoping all this will help!

    Collapse
     
    georgecoldham profile image
    George

    It defiantly will! Do you have an end goal in mind after the bootcamp, or just for fun?

    Also shoot me a message or just ask here anytime if you need help or have any questions, the community is one of the best ways to learn and improve (imo).

    Thread Thread
     
    helen8297 profile image
    Helen Kent

    I hope so! I've been wanting a career change for a while. Toyed with the idea of other bootcamps but just can't afford to quit work & pay for them. Recently discovered the school of code in Bham (schoolofcode.co.uk/) and it looks amazing and is free (double amazing!!)
    Going to apply for that when applications open. All fingers crossed! Thank you for the offer of help - that's very kind and I'll take you up on it!