Primitives, Truthy and Falsey

How does JavaScript compare to Ruby?

JavaScript Primitive Data Types in JavaScript and Ruby

JavaScript Primitive Data Types

A primitive data type in JavaScript is data that is not an object and has no methods.

undefined: the value assigned variables that have been declared, but have no valuenull: Nothing. Something that doesn't exist. The programmer sets a value to null to convey this.boolean: true/falsestring: a sequence of characters used to represent textnumber: a numbersymbol: a unique identifier type, with specific use case, see belowbigint: like numbers but allows large integers beyond the safe integer limit for numbers. Use for big integers.

Undefined vs. null in JavaScript

undefined:let person = {firstName:"John", lastName:"Doe", age:50}
person = undefined // Now both value and type is undefined
console.log(person)
//=> undefined
console.log(typeof(person))
//=> undefined
null:let person = {firstName:"John", lastName:"Doe", age:50}
console.log(person)
//=> {firstName: "John", lastName: "Doe", age: 50}
person = null // Now value is null, but type is still an objectconsole.log(person)
//=> null
console.log(typeof(person))
//=> object

Why is this important?

A common situation arises in programming where the logic of the program based on a whether a value is true or not. For example:

if(someValue) {
doSomething()
}
else {
doSomethingElse()
}
or, more succinctly, with a ternary:
someValue ? doSomething() : doSomethingElse()
let value = undefined //value is undefined
console.log(value ? true : false)
//=>false (undefined is converted to false, making undefined "falsey")
value = null //value is null
console.log(value ? true : false)
//=>false (null is is converted to false, making null "falsey")
value = true
console.log(value ? true : false)
//=>true (true is a boolean, which is "truthy")
value = false
console.log(value ? true : false)
//=>false (false is boolean, which is "falsey")
value = 1
console.log(value ? true : false)
//=>true (1 is a number, converted to true, making 1 "truthy")
value = Symbol()
console.log(value ? true : false)
//=>true (value is a symbol, converted to true, making Symbols "truthy")
value = 2n
console.log( value ? true : false)
//=>true (value is a bigint, converted to true, making BigInts "truthy")
value = 0 //zero
console.log(value ? value = Symbol()
console.log(value ? true : false)
//=>false (0 is the only number converted to false or "falsey")
value = "" //empty string
console.log( value ? true : false)
//=>false (empty string is converted to false or "falsey")
value = null
console.log( value ? true : false)
//=>false (null is converted to false or "falsey")
value = undefined
console.log( value ? true : false)
//=>false (undefined is converted to false or "falsey")
value = Number(undefined)
console.log('value', value) //value NaN
console.log( value ? true : false)
//=>false (NaN is converted to false or "falsey")
value = {}
console.log( value ? true : false)
//=>true ({} is converted to true, or "truthy")
value = []
console.log( value ? true : false)
//=>true ([] is converted to true, or "truthy")

So, how does Ruby do it?

In Ruby there are no primitive data types. Every value is an object. Ruby does implicit conversions as JavaScript does, but slightly differently. Let’s do some tests in the Ruby console to see how things compare.

2.7.0 :001 > value = nil
2.7.0 :002 > value ? true : false
=> false (nil evaluates to false)
(similar to null in JavaScript)
2.7.0 :001 > value = true
2.7.0 :002 > value ? true : false
=> true (true is true)
2.7.0 :005 > value = false
2.7.0 :006 > value ? true : false
=> false (false is false)
2.7.0 :007 > value = 1
2.7.0 :008 > value ? true : false
=> true (1 is converted to true)
2.7.0 :012 > value = {}
2.7.0 :013 > value ? true : false
=> true (an empty hash is converted to true)
2.7.0 :012 > value = []
2.7.0 :013 > value ? true : false
=> true (an empty array is converted to true)
2.7.0 :007 > value = 0
2.7.0 :008 > value ? true : false
=> true (0 is converted to true)
// this is different than in JavaScript
2.7.0 :018 > value = ""
2.7.0 :019 > value ? true : false
=> true ("" is converted to true)
// this is different than in JavaScript

References

https://stackoverflow.com/questions/18790442/are-there-primitive-types-in-ruby
https://www.w3schools.com/js/js_type_conversion.asp

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store