Javascript 101: Let's talk about Variables
Javascript is one of the most popular and powerful programming languages. I have been using javascript for over 3 years now and guess what? I'm still learning as each day passes. I will be starting my series with one of the most important topics in every programming language which is variables.
Let's talk about Variables
The question I always answer when trying to understand/ break things down is "How do I explain this to a 5-year-old"? or "How will a non-programmer understand this"? This is also the approach that will be used to explain most of the topics in this series.
So how will I explain the term "variables" to a non-programmer friend?
Well, I would tell my friend that a variable is a container that stores information. Think of a variable as a box that contains information. You can open the box and replace the information, add something else to the box, or totally remove the information.
In computer programming, a variable has a name and a value. With my definition of variables above, if we relate that to the box. Let's look at a simple example. Think about a box of fruits with different fruits like apples, bananas, and grapes. You can call the box of fruits (variable name) and each fruit can be given a name (value).
Two important things to note about Variables
Variable naming:
» There are two limitations on variable names in JavaScript.
- The name must contain only letters, digits, or the symbols $ and _.
- The first character must not be a digit.
» A variable name should have a clean, obvious meaning, describing the data that it stores.
- When the name contains multiple words, camelCase is commonly used e.g userName
- Use human-readable names like currentTemperature, newPassword, etc.
- Avoid abbreviations or short names like x, y, and z.
- Let your variable names be descriptive and concise.
- In Javascript, all variables are case-sensitive, meaning that GRAPES is not the same as grapes.
- In JavaScript, you cannot use these reserved words as variables, labels, or function names
- Before you can use a variable, you have to create it (this is called declaring a variable).
- A variable should be declared only once. A repeated declaration of the same variable will display an error (syntax error).
- In JavaScript, a variable can be declared using var, let, and const keywords.
- var keyword was used to declare variables when JavaScript was first created. This was the only way to declare variables at that time. This keyword can be confusing and also error-prone.
- let keyword removes the confusion and error of var. It works somewhat differently to var, fixing its issues in the process. It is also the new and recommended way of declaring variables in JavaScript. The let keyword allows you to declare variables that are limited to the scope of a block statement, or expression on which it is used, unlike the var keyword, which declares a variable globally, or locally to an entire function regardless of block scope.
- const keyword is used to declare a constant variable that cannot be changed once assigned a value.
HAPPY CODING!!! ❤️