Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

javascript

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;
like image 469
JadeAmerica Avatar asked Jun 07 '14 03:06

JadeAmerica


1 Answers

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;
 }
like image 148
Dayan Moreno Leon Avatar answered Sep 28 '22 00:09

Dayan Moreno Leon