DEV Community

Cover image for [1] JavaScript Data Types
MV
MV

Posted on

[1] JavaScript Data Types

In addition to my main studies, I constantly watch FrontendMasters courses. Those courses are mostly about JavaScript, HTML, CSS, web development in general, and things like that. So in this post, I’ll share some considerations about JavaScript data types.

Well, at first, there are some basic data types, like strings, numbers, and booleans (true vs false):

console.log(typeof "Test")
→ string

console.log(typeof 8)
→ number

console.log(typeof true)
→ boolean
Enter fullscreen mode Exit fullscreen mode

Then, in the following example, we can see the difference between undefined and undeclared variables. The difference is obvious enough, but at the same time, JavaScript takes both cases as undefined for some reason.

let x
console.log(typeof x) 
→ undefined

console.log(typeof y)
→ undefined (hmm... undefined or maybe undeclared?)
Enter fullscreen mode Exit fullscreen mode

Next, there is a group of quite different things such as an object, array, and null, that are in practice perceived equally.

let x = {}
console.log(typeof x)
→ object

let list = [1, 2, 3]
console.log(typeof list)
→ object (Array is also an object in JS)
Enter fullscreen mode Exit fullscreen mode

I guess that the better way to differentiate regular objects from arrays is to use Array.isArray.

console.log(typeof null)
→ object (Wait... null is an object? 🧐 It seems to be so.)
Enter fullscreen mode Exit fullscreen mode

There were a couple of completely new things to me as well, such as symbol and bigint. They are definitely not the things I used a lot, so I can’t say anything special about them.

console.log(typeof Symbol("&"))
→ symbol

console.log(typeof 1n || BigInt(42))
→ bigint
Enter fullscreen mode Exit fullscreen mode

Of course, there is also a function, which data type is a function. Yes, it was easy this time.

let w = (name) => { console.log(`Hi ${name}`) }
console.log(typeof w)
→ function
Enter fullscreen mode Exit fullscreen mode

And finally, there are also several examples not directly related to JavaScript data types, but which seemed interesting to me.

console.log(Number("Test"))
→ NaN

let zero = -0
console.log(Object.is(zero, 0))
→ false
console.log(Object.is(zero, -0))
→ true
Enter fullscreen mode Exit fullscreen mode

Original post 15.06.20 @ create-react-app.com
Site https://proj.create-react-app.com/
GitHub https://github.com/villivald

Top comments (0)