JavaScript | Variables
TL;DR
This tutorial introduces JavaScript variables as named memory containers for storing and manipulating data, explaining the `var` keyword syntax, strict naming rules including case sensitivity and reserved keywords, and essential conventions like camelCase for writing readable, professional code.
💾 Variables Fundamentals 2 insights
Named Memory Storage
A variable is a named storage location in computer memory that reserves space to store data values for later retrieval and manipulation throughout a program.
Primary Functions
Variables serve four core purposes: storing information for later use, enabling data manipulation, allowing dynamic value changes based on conditions or user input, and improving code readability through semantic labels.
⚙️ Declaration Syntax & Rules 3 insights
Using the var Keyword
Create variables using the syntax `var variableName = value;` where `var` is the reserved declaration keyword, followed by the identifier name and an optional assignment operator with an initial value.
Strict Naming Conventions
Variable names must begin with a letter, underscore, or dollar sign; may subsequently contain letters, digits, underscores, and dollar signs; and are case-sensitive, meaning `name` and `Name` reference two different variables.
Reserved Keywords Restriction
JavaScript prohibits using reserved keywords like `let`, `function`, `delete`, `abstract`, or `var` itself as variable names, though modified versions such as `case_1` are technically valid.
📝 Naming Best Practices 3 insights
Adopt camelCase Formatting
For multi-word variables, use camelCase where the first word is lowercase and subsequent words are capitalized, such as `firstName` or `totalPrice`, rather than snake_case or hyphenated names.
Prioritize Descriptive Names
Choose meaningful, self-documenting identifiers like `isLoggedIn` or `userName` instead of single letters like `x` or cryptic abbreviations like `ilg` to clearly indicate the data's purpose and content.
Avoid Invalid Patterns
Never start variable names with numbers, use hyphens between words, or use reserved keywords, as these practices will trigger syntax errors in JavaScript.
Bottom Line
Always declare JavaScript variables using descriptive camelCase names that start with a letter, utilizing the `var` keyword while strictly avoiding reserved words and special characters except underscores or dollar signs.
More from Company Man
View all
JavaScript | Operators - Part II
This tutorial covers essential JavaScript operators including compound assignment shortcuts for cleaner code, the critical difference between loose and strict equality comparisons, and logical operators (AND, OR, NOT) for boolean logic.
JavaScript | Operators - Part I
This tutorial covers JavaScript's fundamental arithmetic operators including addition, subtraction, multiplication, division, modulus, and exponentiation, with detailed explanations of increment/decrement prefix-postfix differences and operator precedence rules.
JavaScript | Data Types
This tutorial covers JavaScript's seven primitive data types—string, number, boolean, null, undefined, symbol, and BigInt—highlighting that JavaScript uses a single number type for all numeric values and emphasizing the critical distinction between null (intentional absence) and undefined (uninitialized variables).
More in Economics
View all
The Internet Was Weeks Away From Disaster and No One Knew
A mysterious hacker named Jia Tan nearly compromised the entire internet by inserting a backdoor into XZ Utils, a critical compression library maintained by a single