#1 India's Top IT Training Institute
×

About Uncodemy

Know more about who we are and what we stand for.

About Us

About Us

Discover our mission to equip individuals with essential IT skills and industry expertise.

Read More →
UnCodeMy Gallery

UnCodeMy Gallery

Explore highlights from our IT training sessions and success stories.

Read More →
Mentors

Mentors

Meet the industry experts who train and guide you through your course.

Read More →
Free Resources

Free Resources

Free tutorials, interview questions, tools and calculators to help you learn.

Explore →
×

Free Resources

Learn, practice and plan your career — bilkul free.

Tutorials

Tutorials

Step-by-step written tutorials to learn any technology at your own pace.

Read More →
Free Counselling

Free Counselling

Book a free video counselling session and pick the right course for you.

Read More →
Online Compiler

Online Compiler

Write, run and test your code online — no setup required.

Read More →
JavaScript Fundamentals

Primitive Data types

Primitive data types are the simplest kinds of value in JavaScript: single pieces of data that are not objects and have no methods of their own. There are seven of them: string, number, bigint, boolean, undefined, null and symbol. Everything else, including arrays and functions, is an object. Knowing which type a value has tells you how it will compare, copy and behave in calculations.

The seven primitives at a glance

TypeExampleUsed for
string'Delhi', `Hi ${name}`Text
number42, 3.14, NaNIntegers and decimals, one shared type
bigint9007199254740993nWhole numbers too large for number
booleantrue, falseYes or no decisions
undefinedlet x;A value that has not been assigned
nulllet user = null;Intentionally empty
symbolSymbol('id')Unique keys that never clash

Checking a type with typeof

typeof 'hello';     // "string"
typeof 7.5;         // "number"
typeof 10n;         // "bigint"
typeof false;       // "boolean"
typeof undefined;   // "undefined"
typeof Symbol();    // "symbol"
typeof null;        // "object"  (a historical bug, kept for compatibility)

0.1 + 0.2;                  // 0.30000000000000004
(0.1 + 0.2).toFixed(2);     // "0.30"
Number.isNaN(Number('abc')); // true

let a = 'pen';
let b = a;       // b gets its own copy of the value
b = 'pencil';
console.log(a);  // "pen"

Because typeof null reports "object", check for null directly with value === null.

Primitives are immutable and copied by value

A primitive value can never be changed in place. Methods like 'hello'.toUpperCase() return a new string; the original stays the same. When you assign a primitive to another variable, the value is copied, so changing one variable does not affect the other, as the a and b example shows. Objects behave differently: assigning one copies a reference to the same object.

You may wonder how 'hello'.length works if primitives have no properties. JavaScript briefly wraps the primitive in a matching object, such as String, so the method or property can be used, then throws the wrapper away.

Surprises with numbers

All ordinary numbers are stored as 64-bit floating point, so some decimals cannot be represented exactly. That is why 0.1 + 0.2 is not exactly 0.3. For money, a common approach is storing amounts as whole paise or cents and formatting only for display.

NaN ("not a number") is a number that results from failed conversions like Number('abc'). It is not equal to anything, not even itself, so test it with Number.isNaN(). Integers are only exact up to Number.MAX_SAFE_INTEGER; beyond that, use bigint, though you cannot mix bigint and number in arithmetic without converting.

Type conversion can surprise you as well. The + operator joins strings, so '5' + 2 gives '52', while '5' - 2 gives 3. Convert input explicitly with Number() or String() before doing arithmetic.

undefined versus null

Both mean "no value", but they come from different places. JavaScript gives you undefined automatically for unassigned variables, missing properties and missing function arguments. null is something you assign deliberately to say "empty on purpose". Using null for your own empty values keeps the meaning clear.

Remember

There are seven primitives, they are copied by value and never mutated, typeof null lies, and decimal arithmetic needs care.

Ready to master real-world web development?

Learn HTML, CSS3, Bootstrap and JavaScript hands-on with mentor-led sessions.

Explore Course