JavaScript 5 ๐งฌ How to Data Types
5 – How-to Data Types
1. How-to Declare a Boolean Variable
Steps:
- Use
letorconstkeyword - Assign
trueorfalsevalue - Variable holds primitive boolean type
Why: Boolean values represent true/false conditions in logic operations
let isActive = true;
const isComplete = false;
2. How-to Create a String Variable
Steps:
- Use single quotes, double quotes, or backticks
- Assign text content
- Stores text data as primitive type
Why: Strings are essential for text manipulation and display
let greeting = 'Hello World';
const message = "JavaScript is awesome";
const template = `Welcome ${name}`;
3. How-to Declare a Number Variable
Steps:
- Use
letorconst - Assign numeric value
- Stores numeric data as primitive type
Why: Numbers enable mathematical operations and calculations
let age = 25;
const price = 99.99;
const negative = -10;
4. How-to Create an Undefined Variable
Steps:
- Declare variable without assignment
- Or explicitly assign
undefined - Represents no value assigned
Why: Helps identify uninitialized variables or missing data
let emptyVariable;
const notAssigned = undefined;
console.log(emptyVariable); // undefined
5. How-to Assign Null Value to Variable
Steps:
- Use
nullkeyword - Explicitly set variable to null
- Represents intentional absence of object value
Why: Used to clear object references or indicate empty state
let user = null;
const result = null;
6. How-to Create a BigInt Number
Steps:
- Add
nsuffix to numeric literal - Use
BigInt()constructor if needed - Store very large integers
Why: Handles numbers beyond safe integer limits
let bigNumber = 124312435423543645n;
const converted = BigInt(1000);
7. How-to Create a Symbol Value
Steps:
- Use
Symbol()constructor - Optionally add description parameter
- Creates unique immutable value
Why: Ensures unique property keys and private identifiers
let mySymbol = Symbol('description');
const uniqueKey = Symbol();
8. How-to Create an Object
Steps:
- Use curly braces
{} - Add key-value pairs separated by commas
- Keys are strings, values can be any type
Why: Organizes related data and functionality together
let person = { name: 'John', age: 30 };
const car = { brand: 'Toyota', model: 'Camry' };
9. How-to Create an Array
Steps:
- Use square brackets
[] - Add elements separated by commas
- Elements can be any data type
Why: Stores multiple values in a single variable
let colors = ['red', 'blue', 'green'];
const numbers = [1, 2, 3, 4, 5];
10. How-to Create a Function
Steps:
- Use
functionkeyword or arrow syntax - Define parameters (optional)
- Add code block with logic
Why: Reusable blocks of code for specific tasks
function greet(name) {
return 'Hello ' + name;
}
const multiply = (a, b) => a * b;
11. How-to Check Variable Type
Steps:
- Use
typeofoperator - Pass variable name as argument
- Returns string with type name
Why: Debugging and type checking in programs
console.log(typeof 42); // "number"
console.log(typeof 'hello'); // "string"
console.log(typeof true); // "boolean"
12. How-to Access Object Properties
Steps:
- Use dot notation or bracket notation
- Specify key name after object reference
- Retrieve stored value
Why: Accesses specific data within objects
let person = { name: 'Alice', age: 28 };
console.log(person.name); // "Alice"
console.log(person['age']); // 28
13. How-to Add Properties to Objects
Steps:
- Use dot notation or bracket notation
- Assign value to new key
- Object grows with new properties
Why: Dynamically modify object structure
let book = { title: 'JS Guide' };
book.author = 'John Doe';
book['pages'] = 300;
14. How-to Access Array Elements
Steps:
- Use bracket notation with index number
- Index starts at 0 for first element
- Retrieve specific array item
Why: Access individual items from collections
let fruits = ['apple', 'banana', 'orange'];
console.log(fruits[0]); // "apple"
console.log(fruits[2]); // "orange"
15. How-to Modify Array Elements
Steps:
- Use bracket notation with index
- Assign new value to that position
- Update existing array element
Why: Change specific items in collections
let scores = [85, 92, 78];
scores[1] = 95; // Replace 92 with 95
16. How-to Call Functions
Steps:
- Use function name followed by parentheses
- Pass arguments if required
- Execute stored code logic
Why: Reuse code blocks for specific tasks
function add(a, b) {
return a + b;
}
let result = add(5, 3); // 8
17. How-to Return Values from Functions
Steps:
- Use
returnstatement inside function - Specify value to send back
- Function exits with returned value
Why: Pass computed results back to calling code
function calculateTotal(price, tax) {
return price + (price * tax);
}
let total = calculateTotal(100, 0.08);
18. How-to Check if Value is Null
Steps:
- Use strict equality
===with null - Compare variable against null literal
- Returns boolean result
Why: Explicitly verify intentional empty state
let value = null;
if (value === null) {
console.log('Value is explicitly null');
}
19. How-to Check if Value is Undefined
Steps:
- Use strict equality
===with undefined - Compare variable against undefined literal
- Returns boolean result
Why: Verify uninitialized variables or missing values
let empty;
if (empty === undefined) {
console.log('Variable is undefined');
}
20. How-to Create Empty Object
Steps:
- Use empty curly braces
{} - No properties defined yet
- Ready to add key-value pairs
Why: Start with clean object structure
let emptyObject = {};
const data = {};
21. How-to Create Empty Array
Steps:
- Use empty square brackets
[] - No elements inside initially
- Ready to add items later
Why: Start collection with no initial values
let emptyArray = [];
const items = [];
22. How-to Combine Strings
Steps:
- Use
+operator or template literals - Concatenate text portions
- Create new combined string
Why: Build complex text messages from parts
let firstName = 'John';
let lastName = 'Doe';
let fullName = firstName + ' ' + lastName;
let greeting = `Hello ${firstName}`;
23. How-to Perform Mathematical Operations
Steps:
- Use number variables
- Apply arithmetic operators (
+,-,*,/) - Calculate results
Why: Solve numeric problems and computations
let x = 10;
let y = 5;
let sum = x + y; // 15
let product = x * y; // 50
24. How-to Use Symbol as Object Key
Steps:
- Create symbol variable
- Use symbol as property key in object
- Access using bracket notation
Why: Create unique keys that won’t conflict with other properties
let sym = Symbol('unique');
let obj = { [sym]: 'secret value' };
console.log(obj[sym]); // "secret value"
25. How-to Create Function with Parameters
Steps:
- Define function with parameter list
- Use parameters inside function body
- Pass values when calling function
Why: Make functions flexible with input data
function greetUser(name, greeting) {
return `${greeting}, ${name}!`;
}
let message = greetUser('Alice', 'Welcome');
26. How-to Check Array Length
Steps:
- Use
.lengthproperty on array - Access stored count of elements
- Returns number value
Why: Know how many items are in collection
let fruits = ['apple', 'banana'];
console.log(fruits.length); // 2
27. How-to Add Elements to Array
Steps:
- Use
push()method - Pass element as argument
- Append to end of array
Why: Dynamically grow collections with new items
let colors = ['red', 'blue'];
colors.push('green'); // Now has 3 elements
28. How-to Access Object Keys
Steps:
- Use
Object.keys()method - Pass object as argument
- Get array of all keys
Why: Inspect what properties exist in object
let person = { name: 'Bob', age: 35 };
console.log(Object.keys(person)); // ['name', 'age']
29. How-to Check if Array is Empty
Steps:
- Use
.lengthproperty - Compare to zero value
- Return boolean result
Why: Validate that collection has items before processing
let items = [];
if (items.length === 0) {
console.log('Array is empty');
}
30. How-to Get Function Name
Steps:
- Use
.nameproperty on function - Access stored name identifier
- Returns string with function name
Why: Debug and identify functions in code
function myFunction() {}
console.log(myFunction.name); // "myFunction"
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!