undefined
Like most of the languages JavaScript throws an error on access of undefined variable:
foo //Uncaught ReferenceError: foo is not defined
But it does not on property access:
o = {} o.foo //undefined
Array is an object, access by index is property access:
a = []
a[0]
//undefined
And it is possible to assign undefined
:
o = { foo: undefined } o.foo //undefined o.hasOwnProperty('foo') //true
which makes it considerably less useful. By comparison Ruby throws on undefined:
foo #NameError (undefined local variable or method `foo' for main:Object) defined?(foo) #=> nil o = Object.new o.foo #NoMethodError (undefined method `foo' for #<Object:0x000055f2f4e56e78>) a = [] a[0] #=> nil
It is quite easy to implement JavaScript undefined behavior in Ruby.