Relation between null, undefined & undeclared values

Started by arthyk, Oct 01, 2022, 05:20 AM

Previous topic - Next topic

arthykTopic starter

Answer in simple terms: is the value of 'undefinded' in JavaScript equal to zero? In general, what is the difference between a value defined in the language as 'null' and, say, 'undeclared ', and is there a connection between these predefined values?


It's very simple.
the null variable is assigned by the programmer himself
undefined- defined by JS itself
Of course, a programmer can create a variable with the value undefined, but this is very bad coding - you don't need to do this. The variable that the developer creates should look like this:
let a;
either that:
let a = null;
This is the difference, you don't need to know anything extra about it.


undefined is a primitive data type that is assigned to variables as soon as they have been declared. It is also assigned to the arguments of the function that were not passed and is returned from functions inside which there is no 'return value' or there is a 'return' without a value.
The function that is called via the 'new' operator cannot return either.
And also we can set this value explicitly. We can also get it in other cases, usually when the value is undefined.

let exampleUndefined_1;
console.log('empty variable', exampleUndefined_1);
function exampleUndefined_2(a, b) {
    console.log('func arguments', a, b);
function exampleUndefined_3() {

    'Empty return value',
function exampleUndefined_4() {
    let a = 2 + 2;

    'No return value',

You should try to avoid the undefined value until the algorithm for solving your problem requires the opposite.

There are several ways to avoid undefined. The simplest of them is initializing a variable immediately with a value, as well as using let and const instead of var

let foo = 10;    // Good

let bar;        // Bad
bar = 10;

var baz = 10;  // Bad

null is a primitive data type, usually represents the absence of any value in the object.

We'll talk about the objects later. In order to create null, we can assign a null value to a variable or a property of an object.

let nullExample_1 = null;
// Curly braces are used to create an object.
let nullInObjectExample = {
    firstName: null,
    lastName: null,
    age: 10

The difference between null and undefined.
null is a certain value of the absence of an object or the absence of a value for which a key is defined inside the object.
undefined denotes uncertainty, namely that the container was created but its value was not defined or the property was not defined at all.


If you want a simple answer, then it depends on the type of comparison.
It all depends on what type of comparison you are using. Strict (===) or not strict (==).
alert( null === undefined ); // false
alert( null == undefined ); // true
Do not use possible, do not use comparisons >=, >, <, <= with variables that can be "null"/"undefined" if you unsure.