Javascript Introduction

JS Variable and Constant


Javascript Variables

In JavaScript, we can declare variables in three ways: using let, var, and const. 1. var: The var keyword is an older method of variable creation. It is still used to declare variables, but it's considered an outdated approach. var is used to create variables with function scope or global scope, depending on where it's declared. However, it's important to note that using var can lead to unexpected behavior, especially with hoisting and scope issues.

var a = 10 ;
var b = "alpha";
var c = a ;

2. let: The let keyword is also used to create variables in JavaScript. It was introduced in ES6 (ES2015). let is block-scoped, meaning it's only accessible within the block it's declared in. Additionally, variables declared with let are subject to the temporal dead zone, meaning they cannot be accessed before they are declared.

let a = 10 ;
let b = "alpha";
let c = a ;

3. const: The const keyword is also used to declare variables in JavaScript. Once a value is assigned to a const variable, it cannot be changed or reassigned. However, it's important to note that for objects and arrays assigned to a const variable, their properties or elements can still be modified. const variables are also block-scoped, meaning they are only accessible within the block they are declared in. Like let, const was introduced in ES6 (ES2015). For example:

const pi = 3.14159;
console.log(pi); // Output: 3.14159

// Attempting to reassign pi will result in an error
// pi = 3.14; // Error: Assignment to constant variable

// However, modifying properties of an object or elements of an array assigned to a const variable is allowed
const person = { name: 'John', age: 30 };
person.age = 31; // This is valid

const numbers = [1, 2, 3, 4, 5];
numbers.push(6); // This is valid
console.log(numbers); // Output: [1, 2, 3, 4, 5, 6]

Here is the rules for writting a variable in JS.

1. Variable names (identifiers): - Must begin with a letter (a-z, A-Z) or an underscore (_) or a dollar sign ($). - Subsequent characters can be letters, digits (0-9), underscores, or dollar signs. - Variable names are case-sensitive. 2. Reserved keywords: - Cannot be used as variable names since they have special meanings in JavaScript. - Examples of reserved keywords include `let`, `var`, `const`, `function`, `if`, `else`, `return`, etc. 3. No spaces or special characters: - Variable names cannot contain spaces or special characters (except underscores and dollar signs). 4. Meaningful names: - Choose descriptive names that convey the purpose or meaning of the variable. - Avoid using single-letter names or overly cryptic names. 5. CamelCase or snake_case: - Conventionally, variable names are written in camelCase or snake_case for better readability. - camelCase: The first letter of each word is capitalized except for the first word, e.g., `myVariableName`. - snake_case: Words are separated by underscores, e.g., `my_variable_name`. 6. Consistency: - Maintain consistency in naming conventions throughout your codebase for better maintainability. 7. Avoid using global variables: - Minimize the use of global variables to prevent naming conflicts and improve code maintainability. 8. Use `let`, `const`, or `var` for declaration: - Use `let` for variables that need to be reassigned. - Use `const` for variables that should not be reassigned. - Avoid using `var` due to its hoisting behavior and global scope issues. Following these rules ensures that your JavaScript code is clean, readable, and less prone to errors.