JavaScript Fundamentals

Starting Out

  • In a Windows machine, use CTRL+SHIFT+J to open up the console. This is where we'll practice applying JavaScript
  • There are 7 primitive types in total: number, string, boolean, undefined, null, symbol, and bigint.
  • Using the term ** is used for exponentiation: so 2^4 can be expressed as 2**4 in JS. So, 9**2 = 81, 9**3 = 729 etc...
  • NaN means 'Not a Number'. However, if we run the typeof operator on it, it will still be recognized as a number
  • To elaborate on the previous point, something like "200 +(0/0)" will evaluate to NaN.

Variables and let

let

  • In JavaScript, we use:
                            
                                let variableName = value;
                                let numHens = 5;
                                (i.e. so, the number of hens = 5!)
                            
                        
  • As you can see, using let variableName = value; is how we declare our variables in JavaScript!
  • Just as in C++, we can use +=, -=, *=, and /=, ++, and -- for variables in JavaScript.
  • const

  • The const keyword is similar to using const in C++!
  •                     
                            const variableName = value;
                            const daysInWeek = 7;
                        
                    
  • We set variables as constant and don't change them when we use the const keyword!
  • var

  • var is the old way of making variables in JavaScript.
  • It works similarly to let and const, but is considered deprecated.
  • boolean

  • As with C++, we can set variables to true/false. Unlike C++, we don't have to name the type of our variable!
  • let isActive = true; creates a boolean variable, isActive
  • Interestingly, it is possible to change varaible names in JavaScript.
  • Name variables well, and use Camel Case to do so...
  • string

    string

    This variable follows many of the same rules as those we see in C++!

    • One way to find all the functions that you can call upon a string variable is to use the dot operator. Something new is the .trim() function:
                              
                                  let bobbyNoSentence = "   Hey, I'm ready to do it!";
                                  bobbyNoSentence.trim();
                                  //Gives us "Hey, I'm ready to do it!"
                                  bobbyNoSentence.trim().toUpperCase();
                                  //Gives us "HEY, I'M READY TO DO IT!";
                              
                          
      Not the chaining of functions in the above :)