What does the "?" (question mark) mean in javascript? [duplicate] - javascript

This question already has answers here:
Question mark and colon in JavaScript
(8 answers)
Closed 8 years ago.
I'm trying to comment out code that I used from a tutorial but haven't actually seen a ?-mark used in JavaScript...
This is a small part of the code below:
this.year = (isNaN(year) || year == null) ? calCurrent.getFullYear() : year;

What you are referring to is the ternary operator which is an inline conditional statement. To illustrate:
this.year = (isNaN(year) || year == null) ? calCurrent.getFullYear() : year;
is equivalent to
if(isNaN(year) || year == null){
this.year=calCurrent.getFullYear()
}
else{
this.year=year;
}

Related

This code snippet failed on a test of 0, can anyone tell me why and how to fix it [duplicate]

This question already has answers here:
Which equals operator (== vs ===) should be used in JavaScript comparisons?
(48 answers)
Closed 1 year ago.
const dayOfWeek = moment().tz(timezone).format('d');
//test for weekend
// 0 = sunday, 6=saturday
if ((dayOfWeek === 0 ) || (dayOfWeek == 6)
{
response = "closed";
}
else
{
response = "open";
}
this part {dayOfWeek === 0) failed to test TRUE, really not sure why
Thank you for your help.
Double check if dayOfWeek is 0 (number) or '0' (string). I see that for the next check you use == and not ===. Have a look at strict equality

Is it possible to shorten javascript to avoid if and else? [duplicate]

This question already has answers here:
How do you use the ? : (conditional) operator in JavaScript?
(20 answers)
Closed 2 years ago.
I'm trying to convert a PHP snippet in javascript:
function year_shortcode() {
$fromYear = 2010;
$thisYear = (int)date('Y');
return $fromYear . (($fromYear != $thisYear) ? '-' . $thisYear : '');
} add_shortcode('year', 'year_shortcode');
What I've done so far is:
var fromYear='2010';
var thisYear= new Date().getFullYear();
if (fromYear=thisYear) {
document.write(fromYear);
}
else {
document.write(fromYear + '-' + thisYear);
}
I'd like to avoid the if and else statements and shorten it as I would in PHP.
You could do this
And one thing to notice, = is assignment operator, if you want to check equality between two value, use === instead
document.write(fromYear === thisYear ? fromYear : fromYear + '-' + thisYear)
I was trying to use ternary operators as in PHP, thanks to #Pointy and #user4642212 and answer from #hgb123 I was able to solve my question with:
document.write((fromYear != thisYear) ? fromYear + " - " + thisYear : thisYear);

If statement issue enter [duplicate]

This question already has answers here:
Why is my c != 'o' || c != 'x' condition always true? [duplicate]
(5 answers)
Closed 5 years ago.
I have this JS var, followed by this if statement :
if ($('custom_t4'))
var contratType = $('custom_t4').value === 'Nouvelle_affaire' ? 'Nouvelle Affaire' : $('custom_t4').value === 'Avenant' ? 'Avenant' : '';
if (contratType && (contratType !== 'Nouvelle Affaire' || contratType !== 'Avenant')){ }
The problem I have, is that when contratType is defined and his value is 'Nouvelle Affaire', is still enter that if.
Did I miss something ?
In Second condition OR (||) Operator is the problem. Correct Code written below
if (contratType && (contratType !== 'Nouvelle Affaire' && contratType !== 'Avenant')){ }

How to compare String and Symbol in Javascript? [duplicate]

This question already has answers here:
Get the description of a ES6 Symbol
(4 answers)
Closed 5 years ago.
I've a String and a Symbol defined in Javascript with same value Hello, Stack Overflow!. How do I compare them for equality? All the comparisons I've tried below return false.
var string="Hello, Stack Overflow!";
var symbol=Symbol("Hello, Stack Overflow!");
console.log(string == symbol);
console.log(string == symbol.toPrimitive);
console.log(string == symbol.toString);
console.log(string == symbol.toStringTag);
console.log(string === symbol);
console.log(string === symbol.toPrimitive);
console.log(string === symbol.toString);
console.log(string === symbol.toStringTag);
You can convert the string to symbol then perform the comparison.
console.log(Symbol(string).toString() == symbol.toString())
Symbol() will always return a unique value
You can use it slightly differently as below
Symbol.keyFor(Symbol.for("Hello, Stack Overflow!"))== "Hello, Stack Overflow!"; // True
Please read more
https://developer.mozilla.org/en-US/docs/Glossary/Symbol

The kind of jQuery selector [duplicate]

This question already has answers here:
Question mark and colon in JavaScript
(8 answers)
Closed 6 years ago.
I have seen jQuery code's like this
return 13 == t.keyCode ? (t.preventDefault(), !1) : void 0
What that mean for ? and : ?
Please give me reference, because I still newbie in jQuery. Thank's a lot
Its ternary operator.
Shorthand for writting if/else
https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Operators/Conditional_Operator
It is a shorthand for if else:
Translation:
if(13 == t.keyCode) { return (t.preventDefault(), !1); } else { return void 0; }

Categories