Typeof operator in JavaScripts

The typeof operator is used to get the data type (returns a string) of its operand. The operand can be either a literal or a data structure such as a variable, a function, or an object.

Syntax: –
typeof operand
typeof(operand)

Ex: –
typeof “a”;

Undefined

The undefined type is used for variable or object properties that either do not exist or have not been assigned a value. The only value an undefined type can have is undefined.

Null

The null value indicates an empty value; it is essentially a placeholder that represents “nothing”. The null value is defined as an empty object so using typeof operator on a variable holding null shows its type to be an object.

Undefined Vs Null

Undefined means the value hasn‟t been set, whereas null means the value has been set to be empty.

var, let and const

var – The scope of a variable declared with var is its current execution context, which is either the enclosing function or, for variables declared outside any function, global.

let – let allows you to declare variables that are limited in scope to the block, statement, or expression on which it is used.

const – This declaration creates a constant whose scope can be either global or local to the block in which it is declared. Global constants do not become properties of the window object, unlike var variables. An initializer for a constant is required; that is, you must specify its value in the same statement in which it’s declared which can’t be changed later.

Tagged : / / / /

[SOLVED] Flutter : NoSuchMethodError: The getter ‘isEmpty’ was called on null.

Problem

I’m Calling An API and getting response in Variable Data ($data). When data is coming then there is no Error but when I’m getting null as response then getting this error Unhandled Exception: NoSuchMethodError: The getter ‘isEmpty’ was called on null.

When I am using the below code then it is throwing me an error:

Solution

That is because $data returns null, So I’ve Used

if(data?.isEmpty ?? true)

? prevents an error if the previous part of the expression results in null and ?? true results in true if the previous part of the expression is null and therefore treats == null and isEmpty the same for the if(...) check.

Now My Code is working Fine.

Thanks for reading.

Keep Coding.

Tagged : / / / / / /