Posts

Showing posts from January, 2023

Mathematical Assignment Operators

  VARIABLES Mathematical Assignment Operators Let’s consider how we can use variables and math operators to calculate new values and assign them to a variable. Check out the example below: let w =  4 ; w =  w +  1 ; console . log ( w ); // Output: 5 In the example above, we created the variable  w  with the number  4  assigned to it. The following line,  w = w + 1 , increases the value of  w  from  4  to  5 . Another way we could have reassigned  w  after performing some mathematical operation on it is to use built-in  mathematical assignment operators . We could re-write the code above to be: let w =  4 ; w += 1 ; console . log ( w ); // Output: 5 In the second example, we used the  +=  assignment operator to reassign  w . We’re performing the mathematical operation of the first operator  +  using the number to the right, then reassigning  w  to the comput...

Create a Variable: const

  VARIABLES Create a Variable: const The  const  keyword was also introduced in ES6, and is short for the word constant. Just like with  var  and  let  you can store any value in a  const  variable. The way you declare a  const  variable and assign a value to it follows the same structure as  let  and  var . Take a look at the following example: const myName =  'Gilberto' ; console . log ( myName ); // Output: Gilberto However, a  const  variable cannot be reassigned because it is  constant . If you try to reassign a  const  variable, you’ll get a  TypeError . Constant variables  must  be assigned a value when declared. If you try to declare a  const  variable without a value, you’ll get a  SyntaxError . If you’re trying to decide between which keyword to use,  let  or  const , think about whether you’ll need to reassign the variable later on....

Create a Variable: let

  VARIABLES Create a Variable: let As mentioned in the previous exercise, the  let  keyword was introduced in ES6. The  let  keyword signals that the variable can be reassigned a different value. Take a look at the example: let meal =  'Enchiladas' ; console . log ( meal ); // Output: Enchiladas meal =  'Burrito' ; console . log ( meal ); // Output: Burrito Another concept that we should be aware of when using  let  (and even  var ) is that we can declare a variable without assigning the variable a value. In such a case, the variable will be automatically initialized with a value of  undefined : let price ; console . log ( price ); // Output: undefined price =  350 ; console . log ( price ); // Output: 350 Notice in the example above: If we don’t assign a value to a variable declared using the  let  keyword, it automatically has a value of  undefined . We can reassign the value of the variable. Instructions: 1...

Create a Variable: var

  VARIABLES Create a Variable: var There were a lot of changes introduced in the ES6 version of JavaScript in 2015. One of the biggest changes was two new keywords,  let  and  const , to create, or  declare , variables. Prior to the ES6, programmers could only use the  var  keyword to declare variables. var myName =  'Arya' ; console . log ( myName ); // Output: Arya Let’s consider the example above: var , short for variable, is a JavaScript  keyword  that creates, or  declares , a new variable. myName  is the variable’s name. Capitalizing in this way is a standard convention in JavaScript called  camel casing . In camel casing you group words into one, the first word is lowercase, then every word that follows will have its first letter uppercased. (e.g. camelCaseEverything). =  is the  assignment operator . It assigns the value ( 'Arya' ) to the variable ( myName ). 'Arya'  is the  value  assigne...

Variables

Image
  VARIABLES Variables In programming, a  variable  is a container for a value. You can think of variables as little containers for information that live in a computer’s memory. Information stored in variables, such as a username, account number, or even personalized greeting can then be found in memory. Variables also provide a way of labeling data with a descriptive name, so our programs can be understood more clearly by the reader and ourselves. In short, variables label and store data in memory. There are only a few things you can do with variables: Create a variable with a descriptive name. Store or update information stored in a variable. Reference or “get” information stored in a variable. It is important to distinguish that variables are not values; they contain values and represent them with a name. Observe the diagram with the colored boxes. Each box represents variables; the values are represented by the content, and the name is represented with the label. In th...