December 3, 2026 - 08:49
JavaScript Lesson 04 — JavaScript Data Types: Are 3 and
JavaScript Tutorials

JavaScript Lesson 04 — JavaScript Data Types: Are 3 and "3" the Same?

0 Comments

A console can display 3 for both a number and a string. The source matters: 3 is numeric, whereas "3" is text. Their behavior differs as soon as we perform operations.

Inspect the type

JAVASCRIPT
const name = 'Ali';
const count = 3;
const ready = true;
let note;
const selected = null;
console.log(typeof name);
console.log(typeof count);
console.log(typeof ready);
console.log(typeof note);
console.log(selected === null);
console.log(typeof null);

The first four typeof calls produce string, number, boolean and undefined. This helps investigate questions such as why a value was concatenated rather than added. typeof does not change the value; it returns a string describing its type.

The name is quoted text. The count is an unquoted number. The ready flag is the boolean value true. Booleans represent two-state information. Quoting true or false creates a string instead.

Unassigned and deliberately empty are different

let note; declares a binding without supplying an initial value, so its value is undefined. Assigning null explicitly can represent “nothing is currently selected.” Neither is the same as zero or an empty string.

selected === null produces true. The strict-equality operator checks equality without converting types. We will examine comparisons in a later lesson.

The last line is surprising: typeof null returns object. This is a historical language behavior, not a sign that null can be used as an ordinary object. Use an explicit null comparison when you need to identify it.

Does a variable keep one type forever?

JAVASCRIPT
let amount = 3;
console.log(typeof amount);
amount = 'three';
console.log(typeof amount);

The binding name stays the same while its value and type change. Dynamic typing does not make types irrelevant; it makes tracking runtime values important. Reusing a price name for unrelated text can make later calculations confusing.

Const restricts reassignment, not a binding to one specially declared numeric type. Object contents introduce additional details later.

Empty text, zero, null and undefined differ

ValuePossible meaning
''A string of length zero.
0The numeric value zero.
nullA deliberately empty or unselected state.
undefinedAn unassigned or unavailable value.

The application defines the domain meaning. Zero stock is information; missing stock data is a different condition. Treating them identically loses that distinction.

NaN still has type number

JAVASCRIPT
const result = Number('apple');
console.log(typeof result);
console.log(Number.isNaN(result));
console.log(result === NaN);

The results are number, true and false. NaN is a special numeric value, not a useful ordinary quantity. It is not equal to itself under strict equality, so use Number.isNaN rather than comparison with NaN. Number.isFinite answers the separate question of whether a value is a finite number.

Typeof is not a complete classifier

JAVASCRIPT
console.log(typeof []);
console.log(Array.isArray([]));
console.log(typeof null);

An array produces object; Array.isArray identifies it specifically. Null also produces object for historical reasons. The tool is useful, but its output cannot distinguish every structure by itself.

Compare 0 with '0' using typeof, addition and strict equality. Observing behavior as well as appearance makes later error messages easier to interpret.

The same operator can do different work

JAVASCRIPT
console.log(3 + 2);
console.log('3' + '2');
console.log(false);
console.log('false');

The first result is 5; the second is the string 32. Numbers were added and strings were joined. The last two values may look similar in the console, but one is a boolean and one is text. A nonempty string containing “false” does not behave as the false boolean in a condition.

A phone number or postal code can contain only digits without being a quantity to calculate. Leading zeros may matter. Choose a representation according to what the data means, rather than converting every sequence of digits into a number.

Put quotation marks around the count value and inspect its type again. Similar-looking output can hide values that JavaScript treats differently.

References: Reference 1 · Reference 2

Related Articles

Comments (0)

No comments yet. Be the first to comment!

Leave a Comment