Javascript variable undefined outside if else condition [duplicate] - javascript

This question already has answers here:
Chrome/Firefox console.log always appends a line saying 'undefined'
(9 answers)
Closed 9 months ago.
This post was edited and submitted for review 9 months ago and failed to reopen the post:
Original close reason(s) were not resolved
I have 2 objects coming from props and I am assigning these objects to their variable by if else condition
let var1;
let var2;
if (props.id === 1) {
var1 = props;
console.log(var1); // returns {id:1,learn:"HTML", do:"cooking"}
} else {
var2 = props;
}
console.log(var1); // returns {id:1,learn:"HTML", do:"cooking"} and undefined
When I console.log after condition why does it returns the object and undefined? But when console.log inside if condition only returns the object.
I want that it returns only the object if console.log after the condition
My two objects coming like this
{id:1,learn:"HTML", do:"cooking"}
{id:2,learn:"Css", do:"home-work"}
I have seen resources from StackOverflow
Javascript variable is undefined outside if condition
JavaScript Variable undefined outside of function
This question is said to be duplicate for Chrome/Firefox console.log always appends a line saying 'undefined'
but it's completely different it's not about the browsers console I am getting undefined from my javascript file

Use const instead of let and try to differentiate the logs of next render from the previous render like this
const var1;
const var2;
if (props.id === 1) {
var1 = data;
console.log(var1);
} else {
var2 = data;
}
console.log(var1);
console.log("--------");

Related

What is the difference in assigning a processed value directly to a variable and assigning without processing and then processed using && operator [duplicate]

This question already has answers here:
Access Javascript nested objects safely
(14 answers)
Closed 10 months ago.
I saw a function that receives a string parameter then performs some operations with it; like this:
const val = this.searchParam && this.searchParam.trim().toLowerCase();
My question is, why don't assign directly the processed string? Like this:
const val = this.searchParam.trim().toLowerCase();
I tried this in JS Bin to see if there's a difference, and the result is the same.
What do they exactly use the && operator?
In code snippet below, the first log writes undefined, the second throws an error:
searchParam = undefined
console.log(searchParam && searchParam.trim().toLowerCase());
console.log(searchParam.trim().toLowerCase());
Therefore, the result is not the same

How to get rid of the undefined message on Chrome? [duplicate]

This question already has answers here:
Chrome/Firefox console.log always appends a line saying 'undefined'
(9 answers)
Closed 1 year ago.
How to get rid of the undefined message after console.log() a variable
var example = 5;
console.log(example);
output: 5
undefined
You can't:
The reason the debug console outputs undefined after a function, is because you're running the function (example()) and then in the function body, there is no return statement so by default, the return statement will return undefined.
More about return statements at MDN
The reason it does, is: say you have a function called example
function example() {
// Code here
}
Using the return statement inside the function, will return something:
function example() {
// Code here
return "Some string";
}
By default (with no return statement) there will be an undefined return statement.
function example() {
// Code here
}
example(); // undefined
function example() {
// Code here
return true;
}
example(); // true
Also, it does not happen only on Chrome. It happens on all modern and old browsers with JavaScript enabled.

Does A Function Always Return A Value [duplicate]

This question already has answers here:
Does every Javascript function have to return a value?
(4 answers)
Closed 5 years ago.
I have been reading a bit about undefined and just started wondering that we see undefined when we declare a function in browser console.
Does calling a function always return a value? If we do not explicitly return a value, then by default undefined value is returned from a function.
That is to say, a function will always return a value. Always. Is that correct?
var aFunc = function(){
console.log( "aFunc ran." );
}
aFunc() === undefined // true
Yes it always returns a value, if explicit one is not given, it returns undefined. This is same as you will write return undefined or just return.

Why initialization of a variable returns undefined in console? [duplicate]

This question already has answers here:
Why does JavaScript variable declaration at console results in "undefined" being printed?
(4 answers)
Closed 5 years ago.
whenever we define a variable in console with some value for example
var f = 20;
var j = 30;
the above statement returns undefined for one time, can you please help in understanding why it returns undefined even it we have defined both the variables?
Secondly if it is related to hoisting then why undefined is coming only once?
The console reports the result of evaluating the expression, equivalent to typeof basically.
typeof eval("var f = 20;");
returns undefined

Infinite loop in browser's console [duplicate]

This question already has answers here:
Using the variable "name" doesn't work with a JS object
(4 answers)
Closed last year.
Can someone explain why result of this is infinite loop??
var name = true;
var soloLoop = function () {
while (name) {
console.log(name);
name = false;
}
};
soloLoop();
Note 1: Can be reproduced only in browser's console.
Note 2: Is reproducible only with variable "name".
When you declare variables in the global scope, as you're doing here, they're actually contained as properties on the global object, in this case window. window.name is something that already exists, and can only be set to a string.
When you do:
var name = true;
It's actually setting the window.name to "true". Same for name = false - it sets it to "false". Since "false" is "truthy", the while loop will never exit.

Categories

Resources