What Is a Variable?
- A variable holds information and uses a name as a label, while the value stays inside a box.
- Imagine a Real-World Analogy where a labeled storage box keeps Photos, pictures, or other things.
- A locker has a unique number, and people put inside a value or something for storage.
- A variable called score contains 100, and the value can replace the previous value while the label stays the same.
- Variables work the same way because each labeled box uses a unique name to identify stored information.
How Do You Create Variables in JavaScript?
- Creating a variable in JavaScript starts by using the let keyword, followed by the name you chose.
- The = assignment operator puts a value into the box, and the value becomes stored in the variable.
- A score value of 100 shows a simple example, where 100 is the value stored after the assignment.
- The ; Marks the end statement, and it separates one statement from the next in JavaScript.
- A break or new line can display another statement, while the variable continues to give access to the stored value by its name.
What Are the Rules (Must Follow) for Naming Variables?
- Rules for Naming Variable names: Follow names that start with a letter, underscore, or dollar sign.
- Variable names can contain letters, numbers, underscores, and dollar signs, but cannot contain spaces or hyphens.
- Variable names cannot be reserved keywords such as let, if, or function, because JavaScript uses these words for language syntax.
- Variable names are case-sensitive, so Score is different from score.
- These rules support consistent naming, while best practices describe common coding habits rather than language requirements
What Are the Best Practices for Variable Names?
- Best Practices Use descriptive names such as playerScore instead of x, because descriptive names identify stored information more clearly.
- Follow camelCase for multiple words, with examples like firstName and totalAmount.
- Start names with lowercase letters, and Make them meaningful and not too long.
- Good names include userName, totalPrice, isLoggedIn, and itemCount, because each name describes its related value.
- Bad names include x, data, thing, and 2fast, because these names provide limited meaning or do not follow valid naming patterns.
03 · Variables — JavaScript Playground 03Variables
Variables store values you can reuse. Use
letto create one andconsole.log()to read it.Output
What Are the Variables in JavaScript?
- JavaScript has three ways to declare variables: let, const, and var.
- Use let for variables that change later, and let is the Most common choice in modern JavaScript.
- Use const for values that never change, because const stores constants and reassignment causes an Error.
- var is the old way, and using var in modern code is less common than let or const.
- Example: let **score** = 100;, const **maxScore** = 100;, and var player = “Sam”; each works with a different declaration type, while Avoid using var in new examples when explaining modern syntax.
Variables Quiz
Learn how variables store and label data in a program.
0%
What Are the Key Takeaways?
- Variables are named containers that store data and organize program values.
- Create a variable using the assignment operator (=), which assigns values to the variable.
- Use let for values that change, and use const for values that don’t change.
- Variable names are case-sensitive, so score and Score represent different names.
- Choose descriptive, meaningful names because they identify stored data more clearly