JavaScript Coding Day 5 | String, Number, BigInt (Large Integers), Boolean, Undefined, Null
JavaScript Data Types : String, Number, BigInt, Boolean, Undefined, Null
Watch the lesson tutorial 🔻
1. String
A String is simply text. It is a series of characters used to store names, sentences, or any text data. In JavaScript, strings must be surrounded by quotes. You can use double quotes ("") or single quotes ('').
Code Example:
<script>
// A string stores text
let x = "ChiruCodes";
console.log(x);
</script>
Output:
ChiruCodes
2. Number (Integer & Decimal)
In your notes, you referred to this as int. However, in JavaScript, there is only one type for regular numbers: Number. It covers both integers (whole numbers like 50) and decimals (floating-point numbers like 50.5). You don't need to specify int or float.
Code Example:
<script>
// The Number type handles whole numbers and decimals
let x = 50;
let y = 10.5;
console.log(x);
console.log(y);
</script>
Output:
50
10.5
3. BigInt (Large Integers)
Standard JavaScript numbers can only strictly represent integers up to a certain size (specifically $2^{53} - 1$). For "Big Data" or very large accurate calculations, we use BigInt. You create one by appending n to the end of an integer or using the BigInt() function.
Code Example:
<script>
// BigInt is for numbers too large for the standard 'Number' type
let a = BigInt(300);
// You can also write it as: let a = 300n;
console.log(a);
</script>
Output:
300n
4. Boolean
A Boolean represents a logical entity and can have only two values: true or false. These are often used in conditional testing (like if/else statements) to check if something is On/Off or Yes/No.
Code Example:
<script>
// Booleans represent logic: True or False
let b = true;
let isCodingFun = true;
console.log(b);
</script>
Output:
true
5. Undefined
Undefined is the default value of a variable that has been declared (named) but has not yet been assigned a value. If you create a variable box but put nothing inside it, it is undefined.
Code Example:
<script>
// Variable declared, but no value assigned yet
let z;
console.log(z);
// You can also manually set it (though rare)
let y = undefined;
console.log(y);
</script>
Output:
undefined
undefined
6. Null
Null represents an "intentional absence" of any object value. It is something you set manually to tell the program: "This variable is empty on purpose."
Difference:
Undefinedmeans the variable hasn't been touched yet.Nullmeans you explicitly cleared it.
Code Example:
<script>
// We set this to null to show it is intentionally empty
let d = null;
console.log(d);
</script>
Output:
null
- by Chirana Nimnaka

Comments
Post a Comment