Posts

Showing posts from February, 2023

Review Variables

  VARIABLES Review Variables Nice work! This lesson introduced you to variables, a powerful concept you will use in all your future programming endeavors. Let’s review what we learned: Variables hold reusable data in a program and associate it with a name. Variables are stored in memory. The  var  keyword is used in pre-ES6 versions of JS. let  is the preferred way to declare a variable when it can be reassigned, and  const  is the preferred way to declare a variable with a constant value. Variables that have not been initialized store the primitive data type  undefined . Mathematical assignment operators make it easy to calculate a new value and assign it to the same variable. The  +  operator is used to concatenate strings including string values held in variables. In ES6, template literals use backticks  `  and  ${}  to interpolate values into a string. The  typeof  keyword returns the data type (as a string) ...

typeof operator [console.log(typeof variablename)]

  VARIABLES typeof operator While writing code, it can be useful to keep track of the data types of the variables in your program. If you need to check the data type of a variable’s value, you can use the  typeof  operator. The  typeof  operator checks the value to its right and  returns , or passes back, a string of the data type. const unknown1 =  'foo' ; console . log ( typeof unknown1 ); // Output: string const unknown2 =  10 ; console . log ( typeof unknown2 ); // Output: number const unknown3 =  true ; console . log ( typeof unknown3 ); // Output: boolean Let’s break down the first example. Since the value  unknown1  is  'foo' , a string,  typeof unknown1  will return  'string' . Instructions: 1. Use   console.log()   to print the   typeof newVariable . 2. Great, now let’s check what happens if we reassign the variable. Below the   console.log()   statement, reassign   ...

String Interpolation(*** Template Literals***)

  VARIABLES String Interpolation In the ES6 version of JavaScript, we can insert, or  interpolate , variables into strings using  template literals . Check out the following example where a template literal is used to log strings together: const myPet =  'armadillo' ; console . log ( `I own a pet ${ myPet } .` ); // Output: I own a pet armadillo. Notice that: a template literal is wrapped by backticks  `  (this key is usually located on the top of your keyboard, left of the  1  key). Inside the template literal, you’ll see a placeholder,  ${myPet} . The value of  myPet  is inserted into the template literal. When we interpolate  `I own a pet ${myPet}.` , the output we print is the string:  'I own a pet armadillo.' One of the biggest benefits to using template literals is the readability of the code. Using template literals, you can more easily tell what the new string will be. You also don’t have to worry a...

String Concatenation with Variables

  VARIABLES String Concatenation with Variables In previous exercises, we assigned strings to variables. Now, let’s go over how to connect, or concatenate, strings in variables. The  +  operator can be used to combine two string values even if those values are being stored in variables: let myPet =  'armadillo' ; console . log ( 'I own a pet ' +  myPet +  '.' ); // Output: 'I own a pet armadillo.' In the example above, we assigned the value  'armadillo'  to the  myPet  variable. On the second line, the  +  operator is used to combine three strings:  'I own a pet' , the value saved to  myPet , and  '.' . We log the result of this concatenation to the console as: I own a pet armadillo. Instructions: 1. Create a variable named  favoriteAnimal  and set it equal to your favorite animal. 2. Use  console.log()  to print  'My favorite animal: ANIMAL'  to the console. Use st...

The Increment and Decrement Operator

  VARIABLES The Increment and Decrement Operator Other mathematical assignment operators include the  increment operator  ( ++ ) and  decrement operator  ( -- ). The increment operator will increase the value of the variable by 1. The decrement operator will decrease the value of the variable by 1. For example: let a =  10 ; a ++; console . log ( a ); // Output: 11 let b =  20 ; b --; console . log ( b ); // Output: 19 Just like the previous mathematical assignment operators ( += ,  -= ,  *= ,  /= ), the variable’s value is updated  and  assigned as the new value of that variable. Instructions: 1. Using the increment operator, increase the value of   gainedDollar . 2. Using the decrement operator, decrease the value of   lostDollar . Solutions: let gainedDollar = 3 ; let lostDollar = 50 ; gainedDollar ++; lostDollar --;