Course
Introduction
Print Hello WorldAdd Two NumbersFind the Square RootCalculate the Area of a TriangleSwap Two VariablesConvert Kilometers to MilesConvert Celsius to FahrenheitWork With ConstantsWrite to ConsoleControl Flow
Solve Quadratic EquationCheck if a number is Positive, Negative, or ZeroCheck if a Number is Odd or EvenFind the Largest Among Three NumbersCheck Prime NumberPrint All Prime Numbers in an IntervalFind the Factorial of a NumberDisplay the Multiplication TablePrint the Fibonacci SequenceCheck Armstrong NumberFind Armstrong Number in an IntervalMake a Simple CalculatorFind the Sum of Natural NumbersCheck if the Numbers Have Same Last DigitFind HCF or GCDFind LCMFind the Factors of a NumberDisplay Fibonacci Sequence Using RecursionFunctions
Generate a Random NumberFind Sum of Natural Numbers Using RecursionGuess a Random NumberFind Factorial of Number Using RecursionConvert Decimal to BinaryFind ASCII Value of CharacterSet a Default Parameter Value For a FunctionCheck If a Variable is of Function TypePass Parameter to a setTimeout() FunctionPerform Function OverloadingPass a Function as ParameterArrays and Objects
Shuffle Deck of CardsCreate Objects in Different WaysRemove a Property from an ObjectCheck if a Key Exists in an ObjectClone a JS ObjectLoop Through an ObjectMerge Property of Two ObjectsCount the Number of Keys/Properties in an ObjectAdd Key/Value Pair to an ObjectConvert Objects to StringsReplace all Instances of a Character in a StringRemove Specific Item From an ArrayCheck if An Array Contains a Specified ValueInsert Item in an ArrayAppend an Object to an ArrayCheck if An Object is An ArrayEmpty an ArrayAdd Element to Start of an ArrayRemove Duplicates From ArrayMerge Two Arrays and Remove Duplicate ItemsSort Array of Objects by Property ValuesCreate Two Dimensional ArrayExtract Given Property Values from Objects as ArrayCompare Elements of Two ArraysGet Random Item From an ArrayPerform Intersection Between Two ArraysSplit Array into Smaller ChunksCheck If A Variable Is undefined or nullIllustrate Different Set OperationsStrings
Check Whether a String is Palindrome or NotSort Words in Alphabetical OrderReplace Characters of a StringReverse a StringCheck the Number of Occurrences of a Character in the StringConvert the First Letter of a String into UpperCaseCount the Number of Vowels in a StringCheck Whether a String Starts and Ends With Certain CharactersReplace All Occurrences of a StringCreate Multiline StringsFormat Numbers as Currency StringsGenerate Random StringCheck if a String Starts With Another StringTrim a StringCheck Whether a String Contains a SubstringCompare Two StringsEncode a String to Base64Replace All Line Breaks withGet File ExtensionGenerate a Range of Numbers and CharactersRemove All Whitespaces From a TextMiscellaneous
Display Date and TimeCheck Leap YearFormat the DateDisplay Current DateCompare The Value of Two DatesCreate Countdown TimerInclude a JS file in Another JS fileGenerate a Random Number Between Two NumbersGet The Current URLValidate An Email AddressImplement a StackImplement a QueueCheck if a Number is Float or IntegerGet the Dimensions of an ImageConvert Date to NumberJavaScript Program to Perform Function Overloading
To understand this example, you should have the knowledge of the following JavaScript programming topics:
- JavaScript if…else Statement
- JavaScript switch…case Statement
- JavaScript Function and Function Expressions
In programming, function overloading refers to the concept where multiple functions with the same names can have different implementations. However, in JavaScript, if there are multiple functions with the same name, the function that is defined at the last gets executed.
The function overloading feature can be implemented in some other ways.
Example 1: Using if/else-if Statement
// program to perform function overloading
function sum() {
// if no argument
if (arguments.length == 0) {
console.log('You have not passed any argument');
}
// if only one argument
else if (arguments.length == 1) {
console.log('Pass at least two arguments');
}
// multiple arguments
else {
let result = 0;
let length = arguments.length;
for (i = 0; i < length; i++) {
result = result + arguments[i];
}
console.log(result);
}
}
sum();
sum(5);
sum(5, 9);
sum(1, 2, 3, 4, 5, 6, 7, 8, 9);
Output
You have not passed any argument
Pass at least two arguments
14
45
In the above program, the overloading feature is accomplished by using the if/else...if
statement.
- In JavaScript, the
arguments
object is automatically available inside a function that represents the passed arguments to a function. - The multiple conditions are addressed to perform actions based on that particular condition.
Example 2: Using switch Statement
// program to perform function overloading
function sum() {
switch (arguments.length) {
case 0:
console.log('You have not passed any argument');
break;
case 1:
console.log('Pass at least two arguments');
break;
default:
let result = 0;
let length = arguments.length;
for (i = 0; i < length; i++) {
result = result + arguments[i];
}
console.log(result);
break;
}
}
sum();
sum(5);
sum(5, 9);
sum(1, 2, 3, 4, 5, 6, 7, 8, 9);
Output
You have not passed any argument
Pass at least two arguments
14
45
In the above program, the switch
statement is used to accomplish the function overloading functionality. Different conditions result in different actions to be performed.
Also Read: