We have two types of data types in JavaScript . One is Primitive and other one is Non -Primitive data types
In Primitive Data Types , we have :
1) Number (Unlike java , we dont have int,float,double,BigInteger we only have number )
2) Boolean(return True or false)
3)String
4) undefined (when a variable is defined and not assigned a value
then it gives value as undefined)
5)null (absence of any value)
6)bigInt - used to store very large numbers
7) Symbol - Symbols are often used to add unique property keys to an object that won't collide with keys any other code might add to the object, and which are hidden from any mechanisms other code will typically use to access the object. That enables a form of weak encapsulation, or a weak form of information hiding
Non -Primitive Data Types, we have :
1)Array (collection of data of same data type, it is just like java)
2) Object (collection of several data types under a single heading called as Object)
3)Function (when some lines are to be executed several times , functions are used.)
TypeOf:
//typeof (number ,string , null,boolean , undefined)
console.log("data type is ->"+typeof name );
console.log("data type is ->"+typeof number );
console.log("data type is ->"+typeof sachkasamna );
console.log("data type is ->"+typeof nullval );
console.log("data type is ->"+typeof unde );
Top comments (4)
One fact most people miss:
'All types except objects define immutable values (that is, values which can't be changed). For example, Strings are immutable. We refer to values of these types as "primitive values".'
But didn't
value
just mutate?What happened is that the "name"
value
was rebound to a new value. The0
wasn't changed,value
is simply referring to an entirely different value, in this case1
.From that perspective
const
simply means the binding is constant.value
the binding referred to a primitive data type which is always immutable so nothing can be changed.obj
the binding refers to one specific object (whichobj
is permanently bound to). That has no influence on the properties of that object which still can be changed. The same is true for arrays as those are just special objects.Given that
typeof
classifies arrays asobject
hints at the fact that arrays are just objects with a "special power", soArray.isArray()
is necessary to identify them as an array.On the flip side
typeof
on a function comes back asfunction
. For me personally this shows that the statement "functions are first class objects" is a misguided class-based object-oriented perspective. Functions work as objects for the sake of convenience but they are functions first (which can operate as methods) and objects last. That's why I refer to JavaScript as Function-Oriented.Note that
null
was introduced early on to represent "absence of an object" in a planned Java-to-JavaScript bridge. That's whytypeof null
isobject
.The true bottom type of JavaScript is
undefined
. The rule when to use one or the other are just conventions; one convention is to avoidnull
at all cost.They can be used that way. But is also common to use them as tuples: a tuple has a fixed number of elements but each element position can imply a different data type.
Note that
Object.create(null)
can create objects without a prototype; so there is no guarantee thatObject
is in the prototype chain (no-prototype-builtins
).That said the abuse of plain objects as maps is limited as property keys have to be strings or symbols, everything else is coerced via
.toString()
to a string. In general Map makes more sense especially as key deletion is less disruptive.One final piece of advice (from personal experience):
Do not approach JavaScript from Java as a point of familiarity
That is a path of pain and unmet expectations.
JavaScript is its own thing - start from scratch. Brendan Eich was hired intending to do "Scheme in the browser" - later (marketing) directives imposed the JavaScript (formerly Mocha, and then LiveScript) name and the Java-like syntax in the hopes of increasing adoption.
One of the better guides to learning JavaScript is Eloquent JavaScript.
Also be clear on what is "JavaScript" and what is simply a "JavaScript API".
Example: JavaScript data types and data structures
Look at the page's breadcrumbs:
"References > JavaScript > JavaScript data types and data structures"
i.e. this is a JavaScript (programming language) topic
Another example:
EventTarget.addEventListener()
Breadcrumbs:
"References > Web APIs > EventTarget > EventTarget.addEventListener()"
Not a JavaScript topic but it's about the browser based Web API that can be accessed with JavaScript on a browser (or Deno).
The DOM is a Web API—so the nature it's design cannot be directly be blamed on JavaScript (it follows an entirely separate specification).
Nice Summary!
You're missing
bigint
andsymbol
in the primitive data typesthanks for correcting me , Just doing it .