Optional chaining, but for a variable? [duplicate] - javascript

This question already has answers here:
JavaScript check if variable exists (is defined/initialized)
(32 answers)
Closed 12 months ago.
For something like the following:
"use strict";
let a = 1;
console.log(eval(`
a++;
let b=4;
`));
console.log(`a=${a}, b=${b}`);
// ReferenceError: b is not defined
Is it possible to do something where it prints undefined or something if the variable is not there instead of throwing an error? Something conceptually like:
console.log(`a=${a}, b=$?.{b}`)

The placeholder has to contain an expression and there doesn't seem to bean idiomatic way of using an undefined variable (likely due to parsing error) so I would go with a getter instead of using a raw variable.
For example before accessing b you can write
let getB = function () {
return (typeof b !== "undefined")? b :undefined;
}
Then in the template literal you can write ${getB}.

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

Unable to access variable within a function [duplicate]

This question already has answers here:
return a string inside a function
(2 answers)
Closed 1 year ago.
Here is the challenge I need help with:
Create a variable called myFriend using one of the variable declarations described above (var, let, or const). Set the variable you created to contain your friend's name. Inside the greetings function, return the string: "Greetings [your-friend's-name].
New to this, but I can't seem to find out where the error is. Probably easy for you guys but, I'll get there.enter image description here
var myFriend = "Sara"
function greetings() {
let greetings = "greetings, " + myFriend
}
console.log('results: ,'+greetings());
You forgot the return statement.
return greetings;

JavaScript - undefined passed in as a parameter *name*? [duplicate]

This question already has answers here:
What is the purpose of passing-in undefined?
(3 answers)
Closed 5 years ago.
I have just come across a function which accepts a parameter actually called 'undefined'. It is something like this:
(function($,undefined) {
// Do something
})(jQuery);
Either I'm going crazy, or there is no logical reason for this to be here as, well, undefined is undefined is undefined. Please can someone confirm either way? Thanks.
That is a classic trick to have an undefined variable to check against, typically:
if (someVar === undefined) {}
// instead of:
if (typeof someVar === 'undefined') {}
Notice that the wrapper IIFE does not pass any second argument, making the undefined parameter effectively undefined.

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

What does "var app = app || {};" do? [duplicate]

This question already has answers here:
What does var x = x || {} ; [duplicate]
(6 answers)
Closed 9 years ago.
Im looking at some Backbone.js examples and they have var app = app || {}; at the top of all .js files. I understand the literal meaning of this, but what does it do in reference to everything else?
Edit: you guys are really really fast.
It will define the variable app to an empty object if it is not already defined.
This works because being undefined evaluates to false in Javascript.
If it is defined, it may still be re-defined as an empty object if it has a value which evalutes to false, such as an empty string.
The || operator in javascript will return the first operand if it is "truthy". If not, it will return the second operand. If app has not been assigned, it will be undefined, which is "falsey". Thus if it is not defined or is otherwise falsey, an empty object {} will be assigned to app.
This means "define app as an empty object if it's not already defined".
The OR operator in JavaScript does not necessarily yield a boolean. If the left-hand side of the expression yields false then the assignment takes the right-hand side of the expression.
If app is already defined, the it does nothing.
If app is not defined, then it's equivalent to var app = {};

Categories

Resources