Introduction
JavaScript is a high-level programming language widely used in web development, mobile development, and various other domains of software development. In JavaScript, gaining a strong grasp of the fundamental concepts of values and variables is essential. When leveraged effectively, they serve as valuable tools for crafting clean, efficient, and maintainable codebases.
In this article, you will learn about values and variables in JavaScript with detailed examples. You will also learn about the best practices for naming variables in JavaScript.
What are values in JavaScript?
Values are the smallest unit of information in JavaScript. Every piece of data in JavaScript is considered to contain a value. By assigning them to a variable, values can be named and reused, enhancing code reusability. For example, "Susan" is a value. You will see this value using console.log as shown in the example below:
console.log("Susan") // Susan
The code above will output " Susan" in your browser console.
Values have different data types and they are:
Strings
Numbers
Booleans
Arrays
Objects
Undefined
Null
Symbol
What are variables in JavaScript?
Variables are used to store data values and can be accessed throughout the program. They function as containers for holding values and can be updated and retrieved when needed. Without variables, data values cannot be retrieved for later use. Think of a variable as a container that holds a specific value like "Susan". Whenever you need to use the value, you can access it from the container.
How to declare a variable in JavaScript
You can declare a variable in JavaScript using any of the following reserved keywords:
let
var
const
The example below shows how you can use var, let, and const keywords:
let myName
var myName
const myName
How to store data value in a variable
You can store data value to a variable using the assignment operator. In JavaScript, the assignment operator is represented by the symbol "\=". To store a value, place your variable on the left side of the symbol and the desired value on the right side. This process of simultaneously storing a value in a variable is called initializing a variable. The following examples show how to initialize a variable:
let myName = "Susan"
var myName = "Susan"
const myName = "Susan"
Initialized variables can be used over and over again in your codes.
Best practices for naming variables in JavaScript
When naming variables, use descriptive and easy-to-understand names. Variable names should easily describe what value it is holding. Descriptive variable names enhance code readability, reusability, and maintainability.
Below are lists of best practices for naming variables in JavaScript:
- Variable names should always start with either a letter or a dollar sign or an underscore.
let myName = "Susan"
let _myName = "Susan"
let $myName = "Susan"
- Do not begin variable names with a number, doing so will throw an error.
let 3myName = "Susan" // error
- Variable names cannot contain special characters. They can only contain letters, numbers, underscore, and dollar signs.
let first&lastName = "Susan Odii" // error
- Do not name variables with reserved keywords in JavaScript. Using reserved keywords will throw an error
let new = 47 // error
The code above will throw an error because "new" is a reserved keyword in JavaSript.
- It is best to name variables in the camel case. Camel case means, whenever you have multiple words, you write the first letter of the first word with a small letter, then all the first letters of the subsequent words should begin with a capital letter as shown below:
let firstName = "Susan"
let lastName = "Odii"
Notice that the first letter of the first word starts with a small letter and the first letter of the next word starts with a capital letter.
Conclusion
Values and variables are fundamental concepts in JavaScript and having a solid understanding of how to work with them will improve the readability and reuseability of your code.
Variables act as a container for storing values allowing them to be modified and used when the need arises. Initialization occurs when variables are assigned values using the assignment operator.
In JavaScript, following best practices for variables naming will improve code readability and reusability, and continuously honing your skills in working with values and variables will contribute to your growth as a JavaScript developer and help you build robust and high-quality applications.