JavaScript is one of the most popular programming languages in the world, essential for developing modern web applications. One of the foundational concepts in JavaScript is the use of values and variables. This blog post will dive into these concepts, helping you understand how they work and how to use them effectively.
Values
Value is the smallest unit of information in javascript. It's a piece of data. Values can be simple, such as a number or a string, or more complex, like objects and arrays. Here are some examples of different types of values:
Number: 42, 121, 53.53
String: 'Chocolate', "Strawberry", "Cookies N Cream"
Boolean: true, false
Object: { name: 'Josh Anderson', age: 30, hasPets: false }
Array: [1, 2, 3, 4, 5], ["hip-hop", "electronic", "jazz", "pop"]
Variables
A variable is a box where we store our values. We give the box a name and use it wherever we want to use the value in the program.
Declaration: you can declare a variable using var
, let
, or const
keywords. Each keyword has its own scope and usage rules.
var: The
var
keyword is used to declare variables in the global scope or function scope.var user = 'Alexander Wang'; console.log(user); //Output: Alexander Wang
let: The
let
keyword is used to declare block-scoped variables, which means they are only accessible within the block they are defined.let age = 25; if (true) { let age = 30; console.log(age); // Output: 30 } console.log(age); // Output: 25
const: The
const
keyword is used to declare block-scoped constants, which cannot be reassigned once they are defined.const price = 799 console.log(pi); // Output: 799 price = 899; // This will throw an error: "Uncaught TypeError: Assignment to constant variable."
General Rules for Naming Variables (Convention Rules)
When naming variables in JavaScript, following established naming conventions can improve code readability and maintainability. Here are some common conventions and best practices:
Camel Case: Use camelCase for variable names. Start with a lowercase letter and capitalize the first letter of each subsequent word.
let firstName = 'Alex'; let totalAmount = 100;
Using an Underscore: Underscores are often used to denote private variables in object-oriented programming. The naming is simple, use an underscore for every space between two or more words:
let first_Name = 'Alex'; let total_Amount = 100;
Start with a Letter: Variable names should start with a letter, underscore (_), or a dollar sign ($).
let _temp = 25; let $price = 50; let firstName = 'Alexander';
Avoid Reserved Keywords: Do not use JavaScript reserved keywords as variable names (e.g., let, class, return, etc.).
let function = 27 let class = 'ice cream' let return = False console.log(function) //This will throw an error: "SyntaxError: Unexpected token 'function'" console.log(class) //This will throw an error: "SyntaxError: Unexpected token 'class'" console.log(return) //This will throw an error: "SyntaxError: Unexpected token 'return'"
Lastly, Use meaningful and descriptive names that convey the purpose of the variable. Meaningful variable names help make your code more readable and maintainable by providing context about the data it holds or the role it plays in your program.
let firstCustomer = "Julia"
let secondCustomer = "Alex"
"These provide more context for the purpose of naming the varaibles."
let customer1 = "Julia"
let customer2 = "Alex"
"Less descriptive and may cause confusion if these variables
are used in complex logic."
Conclusion
Understanding values and variables is crucial for writing effective JavaScript code. Whether you are using var
, let
, or const
, knowing when and how to use them will help you manage your data more efficiently. Remember to follow best practices for naming and initializing your variables to keep your code clean and readable. Happy Coding! ๐