We help IT Professionals succeed at work.

Uncaught SyntaxError: Missing initializer in const declaration

Jon Imms
Jon Imms asked
on
I am getting the following error in my JS file, "Uncaught SyntaxError: Missing initializer in const declaration" on line 13,14.

The function determines a users' state based on phone-number entered on an html form.

function getState(arecode) {

    // Ensure param is a string to prevent unpredictable parsing results
    if (typeof arecode !== 'string') {
        console.log('Must pass the arecode as a string.');
        return;
    }

  

    // Ensure we don't parse strings starting with 0 as octal values
    const thisarea = parseInt(areacode, 10); 
    const country;
    const state;

    

    // Code blocks alphabetized by state
    if (thisarea == 205 || 938 || 251 || 334) {
        state = 'AL';
        }
    else if (thisarea == 907 || 250 ) {
        state = 'AK';
        }
    else if (thisarea == 480 || 602 || 520 || 928) {
        state = 'AZ';
        }
    else if (thisarea == 501 || 479 || 870) {
        state = 'AR';
        }
    else if (thisarea == 213 || 310 || 424 || 619 || 714 || 415 || 949 || 916 || 408 || 669 || 805 || 510 || 909 || 760 || 209 || 626 || 650 || 951 || 562 || 707 || 925 || 661 || 559 || 530 || 541 || 831 || 858 || 323) {
        state = 'CA';
        }
    else if (thisarea == 720 || 719 || 970) {
        state = 'CO';
        }
    else if (thisarea == 203 || 959) {
        state = 'CT';
        }
    else if (thisarea == 302) {
        state = 'DE';
        }
    else if (thisarea == 786 || 407 || 561 || 813 || 904 || 754 || 727 || 850 || 239 || 321 || 941 || 352 || 772 || 386 || 863 || 785) {
        state = 'FL';
        }
    else if (thisarea == 678 || 770 || 404 || 912 || 762 || 229 || 423 || 478) {
        state = 'GA';
        }
    else if (thisarea == 808) {
        state = 'HI';
        }
    else if (thisarea == 208) {
        state = 'ID';
        }
    else if (thisarea == 847 || 630 || 773 || 312 || 708 || 618 || 217 || 779 || 309 || 872 || 815) {
        state = 'IL';
        }
    else if (thisarea == 463 || 219 || 765 || 574 || 930 || 260) {
        state = 'IN';
        }
    else if (thisarea == 515 || 319 || 712 || 563 || 641) {
        state = 'IA';
        }
    else if (thisarea == 913 || 785 || 316 || 620) {
        state = 'KS';
        }
    else if (thisarea == 859 || 502 || 270 || 606 || 513) {
        state = 'KY';
        }
    else if (thisarea == 504 || 318 || 337 || 985 || 225) {
        state = 'LA';
        }
    else if (thisarea == 207) {
        state = 'ME';
        }
    else if (thisarea == 410 || 240) {
        state = 'MD';
        }
    else if (thisarea == 617 || 978 || 351 || 781 || 339 || 413) {
        state = 'MA';
        }
    else if (thisarea == 248 || 734 || 616 || 313 || 586 || 517 || 989 || 269 || 810 || 231 || 906) {
        state = 'MI';
        }
    else if (thisarea == 612 || 651 || 952 || 763 || 507 || 218 || 320) {
        state = 'MN';
        }
    else if (thisarea == 601 || 662 || 228) {
        state = 'MS';
        }
    else if (thisarea == 314 || 816 || 573 || 417 || 636 || 660 || 479) {
        state = 'MO';
        }
    else if (thisarea == 406) {
        state = 'MT';
        }
    else if (thisarea == 704 || 336 || 910 || 828 || 252 || 919 || 984 || 651) {
        state = 'NC';
        }
    else if (thisarea == 701) {
        state = 'ND';
        }
    else if (thisarea == 402 || 308) {
        state = 'NE';
        }
    else if (thisarea == 702 || 775 || 541) {
        state = 'NV';
        }
    else if (thisarea == 603) {
        state = 'NH';
        }
    else if (thisarea == 732 || 201 || 551 || 908 || 609 || 862 || 973 || 856 || 203 || 606) {
        state = 'NJ';
        }
    else if (thisarea == 505 || 575) {
        state = 'NM';
        }
    else if (thisarea == 718 || 212 || 917) {
        state = 'NY';
        }
    else if (thisarea == 614 || 330 || 216 || 513 || 740 || 937 || 440 || 419 || 567) {
        state = 'OH';
        }
    else if (thisarea == 918 || 405 || 580 || 304) {
        state = 'OK';
        }
    else if (thisarea == 541) {
        state = 'OR';
        }
    else if (thisarea == 610 || 717 || 267 || 814 || 724 || 412 || 570 || 240 || 878) {
        state = 'PA';
        }
    
    else if (thisarea == 401) {
        state = 'RI';
        }
    else if (thisarea == 843 || 803 || 864) {
        state = 'SC';
        }
    else if (thisarea == 605 || 701) {
        state = 'SD';
        }
    else if (thisarea == 615 || 423 || 865 || 901 || 731 || 931 || 456) {
        state = 'TN';
        }
    else if (thisarea == 469 || 281 || 817 || 512 || 210 || 430 || 530 || 956 || 915 || 254 || 806 || 361 || 979 || 409 || 830 || 940 || 936 || 432 || 319 || 325 || 575 || 214) {
        state = 'TX';
        }
    else if (thisarea == 801 || 435) {
        state = 'UT';
        }
    else if (thisarea == 802) {
        state = 'VT';
        }
    else if (thisarea == 757 || 703 || 804 || 540 || 434 || 276) {
        state = 'VA';
        }
    else if (thisarea == 202) {
        state = 'DC';
        }
    else if (thisarea == 206 || 425 || 509 || 360 || 253 || 564) {
        state = 'WA';
        }
    else if (thisarea == 304) {
        state = 'WV';
        }
    else if (thisarea == 608 || 262 || 715 || 920 || 414 || 274) {
        state = 'WI';
        }
    else if (thisarea == 307) {
        state = 'WY';
        }
    else if (thisarea == 204 || 226 || 236 || 249 || 250 || 289 || 306 || 343 || 365 || 367 || 403 || 416 || 418 || 431 || 437 || 438 || 450 || 506 || 514 || 519 || 548 || 579 || 581 || 587 || 604 || 613 || 639 || 647 || 705 || 709 || 778 || 780 || 782 || 807 || 819 || 825 || 867 || 873 || 902 || 905) {
        state = 'Canada';
    }
    else {
        state = 'none';
    }

    return state;
}
    return country;

Open in new window

Comment
Watch Question

NorieAnalyst Assistant

Commented:
Jon

You don't really need to declare state but if you do you should use let to declare it rather than const.

Also, in the if/else if statements you need to repeat thisarea like this, otherwise you''ll always return 'AL'.
if (thisarea == 205 || thisarea == 938 || thisarea == 251 || thisarea == 334) {
        return 'AL';

Open in new window


P.S. You might want to consider something like this to check if the areacode is in the list of areacodes for the state.
    if ([205, 938, 251, 334].indexOf(thisarea)!==-1) {
        state = 'AL';

Open in new window

ste5anSenior Developer

Commented:
A const "variable" cannot change later. Thus you must assign it a value.

But: in your method country and state, which are declared in line 13 and 14 are your result variables. So you need to declare them as var.

When having such a control flow, it is always a good idea to initialize your result variables with the outcome when nothing matches. E.g.

function getState(arecode) {
    var state = '<none>';

    if (typeof arecode !== 'string') {
        console.log('Must pass the arecode as a string.');
    }
    else {
        const thisarea = parseInt(areacode, 10);

        if (thisarea == 205 || 938 || 251 || 334) {
            state = 'AL';
        }
        else if (thisarea == 907 || 250 ) {
            state = 'AK';
        }
        else if (thisarea == 480 || 602 || 520 || 928) {
            state = 'AZ';
        }
    }

    return state;
}

Open in new window

It's called single point of return.
Jon ImmsWeb Developer

Author

Commented:
hi @ste5an

I thought your solution was working but not valid.

If i do this, It always puts AL as the state, regardless what is entered.
function getState(arecode) {
    var state = '<none>';

    if (typeof arecode !== 'string') {
        console.log('Must pass the arecode as a string.');
    }
    else {
        const thisarea = parseInt(areacode, 10);

        if (thisarea == 205 || 938 || 251 || 334) {
            state = 'AL';
        }
        else if (thisarea == 907 || 250 ) {
            state = 'AK';
        }
        else if (thisarea == 480 || 602 || 520 || 928) {
            state = 'AZ';
        }
    }

    return state;
}

Open in new window


I tried this way with @Norie but i just get <none>
function getState(arecode) {
    var state = '<none>';

    if (typeof arecode !== 'string') {
        console.log('Must pass the arecode as a string.');
    }
    else {
        const thisarea = parseInt(areacode, 10);

        if (thisarea == 205 || thisarea == 938 || thisarea == 251 || thisarea == 334) {
            state = 'AL';
        }
        else if (thisarea == 907 || thisarea == 250 ) {
            state = 'AK';
        }
        else if (thisarea == 480 || thisarea == 602 || thisarea == 520 || thisarea ==  928) {
            state = 'AZ';
        }
    }

    return state;
}

Open in new window

Analyst Assistant
Commented:
Jon

The variable you are passing to the function is named arecode.

In the rest of the code you are referring to areacode and thisarea.

Try this.
function getState(areacode) {

    // Ensure param is a string to prevent unpredictable parsing results
    if (typeof areacode !== 'string') {
        console.log('Must pass the arecode as a string.');
        return;
    }

    // Ensure we don't parse strings starting with 0 as octal values
    const thisarea = parseInt(areacode, 10);

    const areaCodes = {'AL':[205,938,251,334],'AK':[907,250],'AR':[501,479,870],'AZ':[480,602,520,928],
                       'CA':[213,310,424,619,714,415,949,916,408,669,805,510,909,760,209,626,650,951,562,707,925,661,559,530,541,831,858,323],
                       'Canada':[204,226,236,249,250,289,306,343,365,367,403,416,418,431,437,438,450,506,514,519,548,579,581,587,604,613,639,647,705,709,778,780,782,807,819,825,867,873,902,905],
                       'CO':[720,719,970],'CT':[203,959], 'DC':[202],'DE':[302],'FL':[786,407,561,813,904,754,727,850,239,321,941,352,772,386,863,785],
                       'GA':[678,770,404,912,762,229,423,478],'HI':[808],'IA':[515,319,712,563,641],'ID':[208],'IL':[847,630,773,312,708,618,217,779,309,872,815],
                       'IN':[463,219,765,574,930,260],'KS':[913,785,316,620],'KY':[859,502,270,606,513],'LA':[504,318,337,985,225],'MA':[617,978,351,781,339,413],
                       'MD':[410,240],'ME':[207],'MI':[248,734,616,313,586,517,989,269,810,231,906],'MN':[612,651,952,763,507,218,320],'MO':[314,816,573,417,636,660,479],
                       'MS':[601,662,228],'MT':[406],'NC':[704,336,910,828,252,919,984,651],'ND':[701],'NE':[402,308],'NH':[603],'NJ':[732,201,551,908,609,862,973,856,203,606],
                       'NM':[505,575],'NV':[702,775,541],'NY':[718,212,917],'OH':[614,330,216,513,740,937,440,419,567],'OK':[918,405,580,304],'OR':[541],'PA':[610,717,267,814,724,412,570,240,878],
                       'RI':[401],'SC':[843,803,864],'SD':[605,701],'TN':[615,423,865,901,731,931,456],'TX':[469,281,817,512,210,430,530,956,915,254,806,361,979,409,830,940,936,432,319,325,575,214],
                       'UT':[801,435],'VA':[757,703,804,540,434,276],'VT':[802],'WA':[206,425,509,360,253,564],'WI':[608,262,715,920,414,274],'WV':[304],'WY':[307]}

    for(state in areaCodes){
        if (areaCodes[state].indexOf(thisarea)!==-1){
            return state;
        }
    }
    
    return 'none';
}

Open in new window

Jon ImmsWeb Developer

Author

Commented:
Thank you so much.  I'm understanding it too