One way to simplify code in JavaScript is with the use of variables. Variables in JavaScript are storage spaces that hold values and will display those values when the variable is called. The let keyword initiates the use of a variable. While var is an older keyword for using variables, JavaScript would still recognize the use both of var and let when using variables. To use variables, you would start by implementing the let keyword followed by a name for the variable. For instance, a variable could look like let myName. In the example I just gave, the variable has only been declared since it does not contain a value. However, if I wanted to initialize the variable, I could give it a value such as let myName = “Aidan”; Here, the variable as been initialized as it now holds a value. If I wanted to call the variable I would just print alert(myName), which would display the text of “Aidan”. When using variables, there are some rules, or guidelines, to follow. The variable can be any length as long as one character is used, the variable can start with a letter, underscore, or even the $ character, but it cannot start with a number. After the first character, a variable ca of any combination of letters, underscores, numbers, and $ characters. Finally, spaces are not allowed within variables. Arithmetic operators can be used within JavaScript with some operators such as addition (+), subtraction (-), multiplication (*), division (/), and modulus (%). These operators help perform mathematical equations with variables. When using these operators, PEMDAS is applied in terms of the order of operations within JavaScript.
Reader Interactions
Comments
Leave a Reply
You must be logged in to post a comment.
Aidan,
Very well-written post. You covered basically every detail needed to understand variables within javascript.
Hello Aiden,
I love the examples and the way you explained them. I myself was a little confused of these but thank you for explaining it and helping me better understand!. I appreciate it!