Function overloading in javascript

Something Like that:
Function overloading

Syntax Parsers: read lang character by character

For that you need to create first html code.

HTML

<p>Lang EN =  <span id="demo1"></span></p>
<p>Lang ES =  <span id="demo2"></span></p>

After that add this JS code.

JS

function greet(firstname, lastname, lang){
    lang = lang || 'en';

    if(lang === 'en'){
        document.getElementById("demo1").innerHTML = 'Hello ' + firstname + ' ' + lastname;
    }

    if(lang === 'es'){
        document.getElementById("demo2").innerHTML = 'Hola ' + firstname + ' ' + lastname;
    }
}

function greetEnglish(firstname, lastname){
    greet(firstname, lastname, 'en');
}

function greetSpanish(firstname, lastname){
    greet(firstname, lastname, 'es');
}

greetEnglish('Johan','Doa');
greetSpanish('Johan','Doe');

[codepen_embed height=”265″ theme_id=”0″ slug_hash=”Nvgqqy” default_tab=”result” user=”pradeepanvi”]See the Pen Function overloading in javascript by Pradeep Kumar (@pradeepanvi) on CodePen.[/codepen_embed]

Now you can see

Lang EN = Hello Johan Doa
Lang ES = Hola Johan Doe

Leave a Reply

Your email address will not be published. Required fields are marked *