33 Javascript Concepts —#2. Primitive values

First of all, this story is based on Stephen Curtis -33 Fundamentals Every Javascript Developer Should Know and you can read it here.
I thought it is great concepts to know although it is not a requirement. So i decided to learn and explain these concepts one by one.

#2. Primitive values

I am going to explain these types except Symbols. if you want to know about Symbols type you can check it here.

Numbers

Unlike other languages, Javascript only has one type of number. It not only includes number like 1 or 1.2, but also includes NaN and Infinity.

Let’s check what i explained with the example below.

console.log(‘1>>>’, typeof 1);
console.log(‘1.2>>>’, typeof 1.2);
console.log(‘Infinity>>>’, typeof Infinity);
console.log(‘NaN>>>’, typeof NaN);
console.log(“‘1’>>>”, typeof ‘1’);
result of example

As you can see, everything except ‘1’ is number type.
Then what is the Infinity and NaN??

console.log(‘1 / 0 >>>’, 1 / 0);
console.log(“1 * ‘d’>>>”, 1 * ‘d’);

Deviding the number by zero results in Infinity and Calculating number with string results in NaN which is short for Not a Number.
I think it’s interesting that Not a Number is Number type. Isn’t it ??

Strings

Strings are literal values in Javascript.

console.log(typeof ‘foo’);
console.log(typeof “foo”);
console.log(typeof `foo`);

Javascript implicitly converts other types of values into string by concatenating them.

console.log(‘foo’ + 77);  
-> console.log(‘foo77’);
console.log(‘foo’ + { name: ‘medium’ });
-> console.log(‘foo{ name: ‘medium’ }’);

Booleans

Boolean represents a logical entity and can have two values : true and false

if (undefined) console.log(‘hi’);
if (null) console.log(‘hi’);
if (‘’) console.log(‘hi’);
if (0) console.log(‘hi’);
if (NaN) console.log(‘hi’);
if (false) console.log(‘hi’);

If you pass undefined, null, empty string, 0, NaN to a conditional statement, they are considered as false since they are falsy value.
(truthy value is everything except falsy value)
As a result, nothing is logged on the console.

Null vs Undefined

Both are definitely falsy value but they are not the same type.
Null types are defined as non-existence.
On the other hand, Undefined types are not defined yet.

let medium;
console.log(medium);
medium = null;
console.log(medium);

If we log the medium before initialization, undefined is logged on the console since it is not defined yet.
Only after defining medium as null, null is logged on the console.

undefined— not defined yet, has no value
null — defined as non-existence, has value (null)

Conclusion

Today we have learned about 5 primitive types.
Definitely, knowing this concept doesn’t boost your javascript skill in a flash.
But i think this basement knowledge will help you become a pro developer.
Hope you guys learn something from this story!!

references : MDN and flow

Junior React Front-end Developer, LinkedIn: https://www.linkedin.com/in/dh-kim-733227200/