xSKILLby Infirals
Register

JavaScript for Beginners

0 of 24 notes read 0%

Reading progress is saved to your INFIRALS account. Sign in to keep this note marked as read.

Types and coercion

Language foundations16 min readLesson 2 of 4

2.1 A value has a type

Every JavaScript value has a type, and the type decides which operations make sense. The primitive types are number, bigint, string, boolean, null, undefined and symbol. Everything else is an object: arrays, functions, dates, maps and the document itself.

Values are not variables. A variable is a name that points at a value. A reassignment moves the name; it does not mutate the old value, because primitives are immutable. An object value is mutable, so two names can point at the same object and see each other's changes.

The typeof operator returns a string naming the type. It is the first tool for debugging a value whose origin is unclear.

javascript
const values = [42, "text", true, undefined, null, {}, []];

for (const value of values) {
  console.log(typeof value);
}

Output

number
string
boolean
undefined
object
object
object

Two results mislead beginners. typeof null is "object", a bug preserved for compatibility, so test null with value === null instead. typeof an array is also "object"; use Array.isArray when the difference matters. Functions are callable objects, and typeof reports them as "function" because their call behaviour deserves its own label.

2.2 Primitive values and references

Primitive values are copied when they are assigned or passed to a function. Change the copy and the original stays the same. Objects behave differently: a variable holds a reference to the object, so two names can point at one object and both see a property change.

The distinction explains a common bug. Passing an array to a function lets the function mutate the caller's array, while passing a number does not change the caller's number. When a function must avoid that, copy the array with spread or map before changing it.

2.3 Numbers

JavaScript has one number type, a 64-bit floating point double. It covers integers and decimals, which keeps the language small but produces famous rounding results. Money should be stored in paise or cents as integers, or formatted through a library, rather than added as floats.

Special values live in the number type too. NaN means not a number and appears when a conversion or calculation has no numeric answer. Infinity results from dividing by zero. Number.isNaN tests for NaN without converting its argument, while the global isNaN converts first and is easy to misuse.

javascript
console.log(0.1 + 0.2);
console.log(Number.isInteger(10));
console.log(Number("42"));
console.log(Number("nope"));
console.log(Number.isNaN(Number("nope")));

Output

0.30000000000000004
true
42
NaN
true

2.4 BigInt and symbol, briefly

Two primitive types rarely appear in beginner code but worth recognising. A bigint is written with an n suffix and holds integers beyond the safe range of the number type; it cannot be mixed with ordinary numbers in arithmetic. A symbol creates a unique key, which libraries use to avoid name collisions on objects.

You will meet Symbol.iterator when a value is looped over, and BigInt when an API returns an identifier that is wider than 53 bits. For this course, knowing that they exist prevents a surprising typeof result from sending you to the wrong fix.

2.5 Strings

A string is a sequence of UTF-16 code units. Single quotes, double quotes and backticks all create strings. Template literals with backticks interpolate expressions with ${...} and allow multi-line text, which makes them the usual choice for building messages.

Strings are immutable. Methods such as toUpperCase return a new string rather than changing the old one. length counts code units, not visible characters, so an emoji can count as two. For most beginner tasks that distinction is invisible, but it explains odd lengths in text from the real world.

javascript
const first = "Aarav";
const last = "Sharma";
const full = `${first} ${last}`;
console.log(full.length);
console.log(full.toUpperCase());
console.log(full.includes("rav"));

Output

12
AARAV SHARMA
true

2.6 Coercion

Coercion is the automatic conversion JavaScript applies when an operator receives values of different types. The + operator is the famous case. If either side is a string, + concatenates and converts the other side to text. With numbers only, it adds.

Other arithmetic operators convert both sides to numbers, so "5" - 2 is 3 while "5" + 2 is "52". Explicit conversion functions send the intended type to the reader: Number, String, Boolean, parseInt and parseFloat. Prefer explicit conversion in code that will be read by someone else in six months.

javascript
console.log("5" + 2);
console.log("5" - 2);
console.log(0 == "0");
console.log(0 === "0");
console.log(Boolean(""));
console.log(Boolean("0"));

Output

52
3
true
false
false
true

The loose equality operator == converts operands before comparing, while strict equality === compares type and value. The pair above shows the trap: 0 == "0" is true, and 0 === "0" is false. Always use === and !== unless you can defend the exception in review.

Truthiness drives every condition. Falsy values are false, 0, -0, 0n, "", null, undefined and NaN. Everything else is truthy, including the string "0" and an empty array. A form that receives "0" from an input is therefore truthy, a classic validation bug.

2.7 null and undefined

undefined means a value was not provided: an unassigned variable, a missing property, or a function with no return. null is a deliberate absence that the programmer sets. JSON supports null and has no undefined, which matters when data crosses an API.

They compare loosely equal and strictly unequal. == gives true because both represent absence, while === gives false because the types differ. Write value == null only when you deliberately mean both, and say so in a comment.

javascript
let later;
const empty = null;
console.log(later);
console.log(empty);
console.log(later == empty);
console.log(later === empty);

Output

undefined
null
true
false

2.8 Coercion inside conditions

Conditions run through truthiness rather than strict comparison. if (input.value) is false for an empty string but true for the string "0", and true for the string "false". A length check or an explicit comparison says what the condition means.

The same rule applies to the logical operators. first || second returns an operand, so a fallback can be a number, a string or an object. When the fallback must apply only to missing values, ?? keeps 0 and false intact. Decide which empty values are meaningful, then choose the operator that matches the decision.

2.9 Common mistakes

  • Using == and accepting a converted match as a correct one.
  • Treating the string "0" as false in a validation check.
  • Comparing floats directly instead of rounding both sides to a fixed precision.
  • Reading a property from an object that may be null or undefined without a guard.
  • Expecting null input to behave like an empty string.

Exam

Type questions usually show an expression and ask for the printed value. Write the type of each operand first, then apply the operator rule, then apply truthiness if a condition is involved. Most wrong answers come from applying arithmetic intuition to a string.

  • Run the typeof example and explain the two misleading object results.
  • Change one `===` to `==` in your own code and find a case where the output changes.
  • List the false values from memory, then check the list.
  • Convert an input string to a number explicitly and check the result.