Search code examples
javascriptnode.jsnullundefinedecmascript-2016

Can a null value automatically occur in JS


In JS an "undefined" value could occur automatically by mistake.(eg:- if you call a function which does not return any thing)

Can a "null" value occur automatically?


Solution

  • undefined is what is returned when you attempt to access a property that is not there or an array value that is not there. It is also the initial value of a variable that has been declared, but not assigned a value yet. These are not really mistakes, but the way the language is designed when you request a value that is not there.

    null does not occur by default like undefined does. If a variable or property or return value is null, that is because some programmer or API specifically assigned or returned null.

    Also, calling a function that does not exist throws a ReferenceError.