1

I don't find the mistake, what am I doing wrong? It has to be a stupid and very easy reason...

When calling this function without a parameter the current year should be used in the following code but I get an 'undefined' error

function get_ostersonntag(year){
    if ((year == "") || (year == null)){
        year= new Date();
        year = year.getFullYear;

    }

    console.log(year)
  }
aejsi5
  • 147
  • 8
  • because `year` is passed with the value `undefined` and hence it doesn't go inside the `if` condition? – Boney May 02 '19 at 08:34
  • but even when I edit the function with year default null the same error occures? function blaala(year=null){etc. – aejsi5 May 02 '19 at 08:36
  • 4
    Well, you miss the parenthesis in `getFullYear();` – Asons May 02 '19 at 08:38
  • What's the error? It works for me (the value of `year` becomes `undefined`, but `undefined == null` is `true`, so the `if` statement gets passed). It logs a function value for me, because `year.getFullYear` is a function - you probably meant `year = year.getFullYear()` – Robin Zigmond May 02 '19 at 08:43
  • 1
    Thank you @LGSon! This was the problem. As mentioned...stupid and very easy reason :) – aejsi5 May 02 '19 at 08:45
  • sorry @LGSon, I'd missed your comment somehow, and just posted mine (with the same content) as an answer :/ – Robin Zigmond May 02 '19 at 08:46

6 Answers6

1

Your function works fine - when you call it with no arguments, the year argument is implicitly assigned to the undefined value. And undefined == null is true, so the if block is executed.

What I suspect is confusing you is that you have returned year.getFullYear, which is a function value. I think you want to actually call this function to get the result:

function get_ostersonntag(year){
    if ((year == "") || (year == null)){
        year= new Date();
        year = year.getFullYear();

    }

    console.log(year)
  }

get_ostersonntag()
Robin Zigmond
  • 17,805
  • 2
  • 23
  • 34
0

You could check if year is falsy, that are values like empty string '', null, undefined, false, but zero 0 as well and NaN and check if year is not zero.

function get_ostersonntag(year) {
    if (!year && year !== 0) {
        return new Date().getFullYear();
    }
    return year;
}

console.log(get_ostersonntag());
console.log(get_ostersonntag(0));
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
0

Thanx all. The reason was the missing brackets after .getFullYear

aejsi5
  • 147
  • 8
0

The only thing you missed are the parentheses after year = year.getFullYear

function get_ostersonntag(year){
    if ((year == "") || (year == null)){
        year= new Date();
        year = year.getFullYear();
    }
    console.log(year)
}
kalehmann
  • 4,821
  • 6
  • 26
  • 36
NoParanoia
  • 24
  • 4
-1

you can try

function get_ostersonntag(year){
    if (!year){
        year= new Date();
        year = year.getFullYear;

    }

    console.log(year)
  }
Harish
  • 1,841
  • 1
  • 13
  • 26
-1

Update your if condition:

function get_ostersonntag(year){
    if (!year) {
        year= new Date();
        year = year.getFullYear();
    }

    console.log(year)
  }

whenever year has falsy value (undefined, null or ''), it will go inside the if loop.

SNag
  • 17,681
  • 10
  • 54
  • 69
Krantisinh
  • 1,579
  • 13
  • 16