Self Assessment Js DataTypes and Variables
Self Assessment Js DataTypes and Variables
In JavaScript, there are several primitive data types. These are the most basic data types that
are not objects and have no methods. Here are the different types of primitive data types in
JavaScript:
1. Number:
o Represents both integer and floating-point numbers.
o Examples: 42, 3.14, -7.
2. BigInt:
o Represents integers with arbitrary precision.
o Used for numbers beyond the safe integer limit for the Number type.
o Example: 1234567890123456789012345678901234567890n.
3. String:
o Represents a sequence of characters.
o Strings are immutable.
o Examples: "hello", 'world', `template literal`.
4. Boolean:
o Represents a logical entity with two values: true or false.
o Examples: true, false.
5. Undefined:
o Represents a variable that has been declared but not assigned a value.
o Example: let a; // a is undefined.
6. Null:
o Represents the intentional absence of any object value.
o Example: let b = null;.
7. Symbol:
o Represents a unique and immutable identifier.
o Often used to add unique property keys to an object that won't collide with
keys from any other code.
o Example: let sym = Symbol('description');.
These primitive data types are the fundamental building blocks in JavaScript and are used to
create more complex data structures and objects.
typeof operator: It can be used to determine the type of a given variable or expression. For
example:
In JavaScript, let and const are both used to declare variables, but they have some key
differences:
let
1. Block Scope: Variables declared with let are block-scoped, meaning they are only
accessible within the block (e.g., { ... }) they are defined in.
2. Reassignment: Variables declared with let can be reassigned to a new value.
3. No Hoisting: Although let variables are hoisted to the top of their block, they are not
initialized. Accessing them before the declaration results in a ReferenceError.
let x = 10;
x = 20; // Allowed
{
let y = 30;
console.log(y); // 30
}
console.log(y); // ReferenceError: y is not defined
const
1. Block Scope: Like let, variables declared with const are block-scoped.
2. No Reassignment: Variables declared with const cannot be reassigned to a different
value after their initial assignment. However, if the variable is an object or array, the
properties or elements can still be modified.
3. No Hoisting: Similar to let, const variables are hoisted to the top of their block but
are not initialized. Accessing them before the declaration results in a
ReferenceError.
Example:
const a = 10;
a = 20; // TypeError: Assignment to constant variable.
{
const c = 30;
console.log(c); // 30
}
console.log(c); // ReferenceError: c is not defined
Summary
Use let when you need to reassign a variable within its block scope.
Use const when you want to ensure a variable is not reassigned, but be aware that
objects and arrays declared with const can still have their contents modified.
Explicit Coercion
Explicit coercion occurs when you manually convert from one type to another using
JavaScript functions or operators.
Examples:
1. String to Number:
2. Number to String:
3. Boolean to String:
Implicit Coercion
Implicit coercion happens automatically when JavaScript expects a certain type but receives
another.
Examples:
3. Boolean Coercion:
Coercion Rules
1. . To Boolean:
Falsy Values: false, 0, -0, 0n (BigInt zero), "" (empty string), null, undefined, and NaN.
Truthy Values: Everything else.
Boolean(""); // false
Boolean("hello"); // true
Boolean(0); // false
Boolean(42); // true
2. To Number:
Number("123"); // 123
Number("abc"); // NaN
Number(""); // 0
Number(null); // 0
Number(undefined); // NaN
3. To String:
String(123); // "123"
String(true); // "true"
String(null); // "null"
String(undefined); // "undefined"
Common Implicit Coercion Scenarios
1. Equality Operator (==): The == operator can coerce types to compare values.
"5" == 5; // true
0 == false; // true
null == undefined; // true
"6" * "2"; // 12
"6" / "2"; // 3
"6" - 2; // 4
3. Logical Operators: Logical operators (&&, ||, !) convert their operands to booleans.
To avoid unexpected results due to type coercion, it's generally good practice to use the strict
equality operator (===) and explicit type conversion functions.
Example:
let num = "5";
if (num === 5) {
// This will not run, as num is a string and 5 is a number.
}
if (Number(num) === 5) {
// This will run, as num is explicitly converted to a number.
}
Understanding and managing type coercion in JavaScript helps in writing more predictable
and bug-free code.
4o
*Quiz:*
1. String
2. Number
3. Boolean
4. Undefined
5. Null
6. Symbol (added in ES6)
7. BigInt (added in ES11)
Objects, on the other hand, are not primitive data types. They are complex data structures that
can contain collections of data and more complex entities.
In JavaScript, the typeof operator returns "object" for null. This is actually considered a bug in the
language, but it has been kept this way for backward compatibility reasons. So, even though null is
a primitive value, typeof null will return "object".
In JavaScript, the const keyword is used to declare a variable that cannot be reassigned. Variables
declared with const are block-scoped and must be initialized at the time of declaration.
In JavaScript, when you use the + operator with a number and a string, the number is coerced into a
string and the two strings are concatenated. Therefore, 2 + "2" results in the string "22".
In JavaScript, the let keyword allows you to declare a variable that can be reassigned. Therefore,
the code let a = 10; a = 20; will successfully reassign the value of a to 20.
You can check if a variable is undefined using any of the following methods:
1. variable == undefined: This works because undefined is a special value that has
a loose equality with undefined. However, this approach may give unexpected
results if the variable has been explicitly set to null or another value.
2. typeof variable === "undefined": This is the safest way to check for undefined
because it doesn’t throw an error if the variable hasn’t been declared. It works even if
the variable is undeclared.
3. variable === undefined: This is a strict comparison that works if the variable is
explicitly declared and not assigned a value. However, it may throw an error if the
variable hasn't been declared.
All three methods are valid, but typeof variable === "undefined" is generally
recommended for its safety and reliability.
In JavaScript, dividing a positive number by zero results in Infinity. If you divide a negative
number by zero, you get -Infinity. If you divide zero by zero, you get NaN (Not-a-Number).
- a) 5
- b) 10
- c) Error
- d) undefined
The const keyword is used to declare variables that are meant to be constant, meaning they
cannot be reassigned after their initial assignment. Trying to reassign x from 5 to 10 will
cause a TypeError and prevent the console.log(x) line from executing.