JavaScript 4 ๐งฌ How to Variable declaration
4 – How-to Variable declaration
1. How to declare a string variable using single quotes
Steps:
- Use
letorconstto declare the variable. - Assign a value inside single quotes (
'...'). - The value will be stored as a string type.
let greeting = 'Hello, World!';
2. How to declare a string variable using double quotes
Steps:
- Use
letorconstto declare the variable. - Assign a value inside double quotes (
"..."). - Works similarly to single quotes.
let name = "Alice";
3. How to declare a number variable with an integer
Steps:
- Declare using
let,const, orvar. - Assign an integer value (no decimal point).
- It will be stored as a number type.
let age = 30;
4. How to declare a floating-point number
Steps:
- Declare using
let,const, orvar. - Assign a value with a decimal point.
- Stored as a number type.
let height = 5.9;
5. How to use underscores in large numbers for readability
Steps:
- Use underscores (
_) between digits. - This helps make large numbers more readable.
- Not used during runtime โ just for clarity.
let population = 7_891_024_000;
6. How to declare a boolean variable with true
Steps:
- Declare using
let,const, orvar. - Assign the value
true. - Boolean values can be either
trueorfalse.
let isStudent = true;
7. How to declare a boolean variable with false
Steps:
- Declare using
let,const, orvar. - Assign the value
false. - Used for logical checks in code.
let isLoggedIn = false;
8. How to declare an undefined variable
Steps:
- Use
letorvar. - Declare without assigning a value.
- Its value is automatically set to
undefined.
let x;
console.log(x); // undefined
9. How to assign null explicitly to a variable
Steps:
- Use
letorconst. - Assign the keyword
null. - Represents intentional absence of any object value.
let y = null;
10. How to create a symbol using Symbol() constructor
Steps:
- Use
constto declare. - Call
Symbol()with an optional description. - Symbols are unique and immutable.
const id1 = Symbol('id');
11. How to declare a BigInt using BigInt() constructor
Steps:
- Use
const. - Wrap the number in
BigInt(...). - For very large integers that exceed normal number limits.
const bigIntExample = BigInt(999_999_999_999_999_999);
12. How to declare a BigInt using suffix notation
Steps:
- Add
nat the end of a numeric literal. - Ensures it is treated as a BigInt type.
const anotherBigInt = 1234567890123456789012345678901234567890n;
13. How to declare an object with properties
Steps:
- Use
letorconst. - Create an object using curly braces
{}. - Define keys and values inside.
let person = {
name: 'Bob',
age: 25,
};
14. How to define a method within an object
Steps:
- Inside the object, add a key with a function as its value.
- Use
thisto refer to the object’s properties.
let person = {
name: 'Bob',
greet: function() {
return "Hello, my name is " + this.name;
}
};
15. How to declare an array using square brackets
Steps:
- Use
letorconst. - Place elements inside square brackets separated by commas.
- Arrays can hold different data types.
let fruits = ['apple', 'banana', 'cherry'];
16. How to declare a function using function keyword
Steps:
- Use
functionfollowed by the name and parameters. - Define what the function does inside curly braces.
- Return a result if needed.
function multiply(a, b) {
return a * b;
}
17. How to use let for block-scoped variables
Steps:
- Use
letinstead ofvar. - The variable is only accessible within the block
{}. - Introduced in ES6.
if (true) {
let message = "Hello";
}
// message cannot be accessed outside the if block
18. How to declare a constant using const
Steps:
- Use
constto declare a variable. - Must initialize with a value at declaration time.
- Cannot reassign after initial assignment.
const PI = 3.14;
// PI = 3.15; // Error!
19. How to use camelCase for naming variables
Steps:
- Start with lowercase letter.
- Capitalize the first letter of subsequent words.
- Avoid spaces or special characters.
let myVariable = "value";
20. How to avoid starting variable names with digits
Steps:
- Variables must start with a letter, underscore (
_), or dollar sign ($). - Never begin with a digit.
let _myVar = 'valid';
let $price = '$5'; // valid too
// let 1variable = "invalid"; // SyntaxError
21. How to distinguish between uppercase and lowercase in variable names
Steps:
- JavaScript is case-sensitive.
MyVar,myVar, andMYVARare all different.
let myVar = 10;
let MyVar = 20;
console.log(myVar); // 10
22. How to declare a variable with no initial value
Steps:
- Declare using
let. - Leave the assignment part empty.
- Automatically becomes
undefined.
let test;
console.log(test); // undefined
23. How to combine multiple variables in one declaration
Steps:
- Use comma-separated assignments with
letorconst. - Useful for grouping related variables.
let a = 1, b = 2, c = 3;
24. How to reassign a let variable
Steps:
- Declare using
let. - Reassign new values later in the code.
- Unlike
const, this is allowed.
let count = 5;
count = 10; // OK
25. How to prevent reassignment with const
Steps:
- Declare using
const. - Once assigned, it cannot be changed.
- Useful for constants like mathematical values.
const MAX_USERS = 100;
// MAX_USERS = 200; // Error!
26. How to use var in function scope
Steps:
- Use
varinside functions. - Variables are scoped to the function or globally if outside any block.
function example() {
var localVar = 'Hello';
}
// localVar is not accessible here
27. How to use var in global scope
Steps:
- Declare
varoutside any function or block. - Available throughout the entire script.
var globalVar = 'I am global';
Stop using slow, ad-bloated tool sites! ๐คฎ
๐ Search “KandZ Tools” on Google to use many professional utilities for free.
KandZ.me is the ultimate minimalist hub for:
โ
Finance (Mortgage, Interest, Inflation)
โ
Tech (Base64, JSON, Dev Suite, IP)
โ
Health (BMI, BMR, TDEE)
โ
Productivity (Timer, Workspace, QR)
โก๏ธ Fast & Private
๐ No data leaves your device
๐ 100% Free
๐ Use it now: https://tools.kandz.me
๐ Bookmark itโyouโll need it later!