3. Data Types & Type Checking

1. Primitive Data Types – Examples

( Number, String, Boolean, Undefined, Null, BigInt, Symbol )

Program 1: Basic Primitive Types

				
					<!DOCTYPE html>
<html>
<body>
<script>
let age = 25;              // Number
let name = "JavaScript";  // String
let isStudent = true;     // Boolean

document.write(age + "<br>" + name + "<br>" + isStudent);
</script>
</body>
</html>

				
			

Program 2: Undefined and Null

				
					<script>
let x;              // Undefined
let y = null;       // Null

document.write(x + "<br>" + y);
</script>

				
			

Program 3: BigInt and Symbol

				
					<script>
let bigNum = 12345678901234567890n;
let id = Symbol("id");

document.write(bigNum + "<br>" + id.toString());
</script>

				
			

2. Non-Primitive Data Types – Examples

( Object, Array, Function )

Program 1: Object Example

				
					<script>
let student = {
  name: "Amit",
  age: 22
};

document.write(student.name + " - " + student.age);
</script>

				
			

Program 2: Array Example

				
					<script>
let marks = [80, 85, 90];
document.write(marks);
</script>
Program 3: Function Example
<script>
function greet() {
  return "Hello!";
}

document.write(greet());
</script>

				
			
  1. Use of typeof Operator

Program 1: Primitive Types

				
					<script>
let a = 10;
let b = "JS";
let c = true;

document.write(typeof a + "<br>" + typeof b + "<br>" + typeof c);
</script>

				
			

Program 2: Special Cases

				
					<script>
let x;
let y = null;

document.write(typeof x + "<br>" + typeof y);
</script>

				
			

Program 3: Non-Primitive Types

				
					<script>
let arr = [];
let obj = {};

document.write(typeof arr + "<br>" + typeof obj);
</script>

				
			

4. Difference Between null and undefined

Program 1: Basic Difference

				
					<script>
let a;
let b = null;

document.write("a = " + a + "<br>");
document.write("b = " + b);
</script>

				
			

Program 2: Using typeof

				
					<script>
let a;
let b = null;

document.write(typeof a + "<br>" + typeof b);
</script>

				
			

Program 3: Comparison

				
					<script>
let a;
let b = null;

document.write(a == b);   // true
document.write("<br>");
document.write(a === b);  // false
</script>

				
			

5. Type Coercion (Implicit Conversion)

Program 1: String + Number

				
					<script>
let result = "5" + 10;
document.write(result);
</script>

				
			

Program 2: Number Conversion

				
					<script>
let result = "5" - 2;
document.write(result);
</script>

				
			

Program 3: Boolean Conversion

				
					<script>
document.write(true + 1 + "<br>");
document.write(false + 1);
</script>

				
			

6. Type Conversion Using Number(), String(), Boolean()

Program 1: Number() Conversion

				
					<script>
let a = "123";
let b = Number(a);

document.write(b + "<br>" + typeof b);
</script>

				
			

Program 2: String() Conversion

				
					<script>
let num = 50;
let str = String(num);

document.write(str + "<br>" + typeof str);
</script>

				
			

Program 3: Boolean() Conversion

				
					<script>
document.write(Boolean(1) + "<br>");
document.write(Boolean(0) + "<br>");
document.write(Boolean(""));
</script>