Talk is Cheap, Show me the Code - Linus Torvalds
Introduction
What are Variables?
- Variables store information in a JavaScript program.
- They act like labeled boxes to hold data.
Creating Variables
- Use let to create a variable.
- Example:
let message;
Assigning Values
- Put data into a variable using =.
- Example:
let message = 'Hello';
Accessing Data
- Retrieve data from a variable using its name.
- Example:
let message = 'Hello';
alert(message); // Shows the variable content
Declaring Multiple Variables
- Declare multiple variables on separate lines for better readability.
- Example:
let user = 'John'; let age = 25; let message = 'Hello';
Variable Naming Rules
- Variable names contain letters, digits, $, and _.
- The first character can't be a digit.
- Use camelCase for multiple words.
- Examples:
let userName; let test123;
Constants
- Declare constants using const.
- Constants cannot be changed after assignment.
- Example:
const myBirthday = '18.04.1982';
Uppercase Constants
Use uppercase and underscores for constants with known values.
Example:
const COLOR_RED = "#F00";
Variable Naming Best Practices
- Choose clear and meaningful variable names.
- Avoid short or cryptic names.
- Make your code self-explanatory.
- Example:
let currentUser;
let shoppingCart;
Avoid Reusing Variables
- Create new variables instead of reusing existing ones.
- Clear, separate variables lead to better code quality.
JavaScript Data Types
1. String
- Used for text data.
- Enclosed in single (''), double ("") quotes, or backticks (``).
- Example:
const name = 'John';
2. Number
- Represents whole numbers or decimals.
- Example:
const age = 25;
const price = 19.99;
3. BigInt
- For very large numbers, append 'n' to the end.
- Example:
const bigIntValue = 900719925124740999n;
4. Boolean
- Represents true or false values.
- Used for logical decisions.
- Example:
const isRaining = true;
5. undefined
- Represents uninitialized variables.
- Example:
let lastName;
6. null
- Represents an empty or unknown value.
- Example:
const emptyValue = null;
7. Symbol
- Unique and immutable data type.
- Introduced in newer JavaScript versions (ES2015+).
- Example:
const uniqueSymbol = Symbol('description');
8. Object
- Used to store collections of data.
- Consists of key-value pairs.
- Example:
const person = { name: 'Alice', age: 30 };
JavaScript Type
- JavaScript determines data types automatically.
- A variable can change its type during execution.
JavaScript typeof
- Use typeof operator to check the type of a variable.
- Example:
typeof(name); // returns "string"
typeof(age); // returns "number"
typeof(isRaining); // returns "boolean"
typeof(uniqueSymbol); // returns "symbol"
Use it as a quick Go through in case of Revision, etc.
Thanks!
Also, Follow me for upcoming articles on JS based on my Learnings :)
Top comments (0)