javascript

Null, Undefined and “Not defined” in Javascript

In JavaScript, null, undefined, and not defined are three distinct concepts that refer to different values or states. In this article we shall look at null, undefined and not defined in Javascript and their unique use cases

Null

Null is the intentional absence of any object value. This could be a property of an object, or an expected value of a variable.

Example

let person = {
"name": "Lawson Luke",
"hobby": null
}
console.log(person.hobby); //null

When validating user inputs, values can also be checked for null values

let email = null;
if(email === null){
alert('Please enter your email');
}

Undefined

Undefined, in the simplest term, means that a property of variable is not initialized or does not exist.

let person = {
"name": "Lawson Luke",
"hobby": "coding",
}
console.log(person.age); //undefined

Not defined

“Not defined” refers to a variable that is not declared at any given point in time with the declaration keyword like var, let or const.

console.log(name); //Uncaught ReferenceError: name is not defined

It is important to note that null and undefined are distinct values, but they are both falsy values in JavaScript, which means they will evaluate to false in a Boolean context. Also, not defined is an error that occurs when you try to reference a variable or function that has not been declared

Conclusion

Now we know the difference between null, undefined and “not defined” in Javascript, and we can begin to write cleaner code and better logic.

Javascript search API

Author

Recent Posts

Apple is developing a doorbell camera equipped with Face ID technology.

Apple is reportedly developing a new smart doorbell camera with Face ID technology to unlock…

4 hours ago

Google Launches Its Own ‘Reasoning’ AI Model to Compete with OpenAI

This month has been packed for Google as it ramps up efforts to outshine OpenAI…

2 days ago

You can now use your phone line to call ChatGPT when cellular data is unavailable.

OpenAI has been rolling out a series of exciting updates and features for ChatGPT, and…

4 days ago

Phishers use fake Google Calendar invites to target victims

A financially motivated phishing campaign has targeted around 300 organizations, with over 4,000 spoofed emails…

4 days ago

Hackers Exploiting Microsoft Teams to Remotely Access Users’ Systems

Hackers are exploiting Microsoft Teams to deceive users into installing remote access tools, granting attackers…

6 days ago

Ethical Hacking Essentials

Data plays an essential role in our lives.  We each consume and produce huge amounts…

7 days ago