Best Practices for Function Overloading in JavaScript

Nitya Raut
Updated on 16-Jun-2020 11:42:02

282 Views

Function overloading occurs when a function performs different tasks based on a number of arguments passed to it.The best practice for function overloading with parameters is to not check the types. The code runs slower when the type is checked and it should be avoided. For this, the last parameter to the methods should be an objectAlso, do not check the argument length.ExampleHere’s an example −function display(a, b, value) { } display(30, 15, {"method":"subtract"}); display(70, 90, {"test":"equals", "val":"cost"});

Reverse Elements of an Array Using Stack in Java

Ramu Prasad
Updated on 16-Jun-2020 11:41:10

4K+ Views

Stack is an Abstract Data Type (ADT), commonly used in most programming languages. It is named stack as it behaves like a real-world stack, for example – a deck of cards or a pile of plates, etc.A stack is first in first out, it has two main operations push and pop. Push inserts data in to it and pop retrieves data from it.To reverse an array using stack initially push all elements in to the stack using the push() method then, retrieve them back using the pop() method into another array.ExampleLive Demoimport java.util.Arrays; import java.util.Stack; public class ReversinArrayUsingStack { ... Read More

Use of Semicolon After Every Function in JavaScript

radhakrishna
Updated on 16-Jun-2020 11:41:07

828 Views

Adding semicolons after every function is optional. To avoid undesirable results, while using functions expressions, use a semicolon.Using semicolonvar display = function () {   alert(“Hello World”); }; (function () {   // code })();Without using semicolonvar display = function () {   alert(“Hello World”); } (function () {   // code })();The first function executes immediately since we have used the semicolon.

Write a Global Error Handler in JavaScript

mkotla
Updated on 16-Jun-2020 11:40:30

2K+ Views

The following global error handler will show how to catch unhandled exception −Example                    window.onerror = function(errMsg, url, line, column, error) {             var result = !column ? '' : 'column: ' + column;             result += !error;             document.write("Error= " + errMsg + "url= " + url + "line= " + line + result);             var suppressErrorAlert = true;             return suppressErrorAlert;          };          setTimeout(function() {             eval("{");          }, 500)          

Default Values of Instance Variables in Java

Amit Sharma
Updated on 16-Jun-2020 11:40:00

2K+ Views

When we haven’t initialized the instance variables compiler initializes them with default values.For boolean type, the default value is false, for float and double types default values are 0.0 and for remaining primitive types default value is 0.ExampleLive Demopublic class Sample {    int varInt;    float varFloat;    boolean varBool;    long varLong;    byte varByte;    short varShort;    double varDouble;    public static void main(String args[]){       Sample obj = new Sample();       System.out.println("Default int value ::"+obj.varInt);       System.out.println("Default float value ::"+obj.varFloat);       System.out.println("Default boolean value ::"+obj.varBool);     ... Read More

Style Bootstrap 4 Cards with bg-danger Class

Amit Diwan
Updated on 16-Jun-2020 11:38:39

335 Views

To color Bootstrap cards, we use the contextual classes.For danger action, use the bg-danger contextual class with the card class −   Danger! High Voltage! You can try to run the following code to implement the card-danger class −ExampleLive Demo       Bootstrap Example                             Messages           How you doing?              Danger! High Voltage!      

HTML 5 Standard Events

Giri Raju
Updated on 16-Jun-2020 11:38:33

708 Views

When a user visits your website, they do things like click on text and images and given links, hover over things etc. These are examples of what JavaScript calls events.We can write our event handlers in JavaScript or VBScript and you can specify these event handlers as a value of event tag attribute. The HTML5 specification defines various event attributes as listed below −AttributeValueDescriptionOfflinescriptTriggers when the document goes offlineOnabortscriptTriggers on an abort eventonafterprintscriptTriggers after the document is printedonbeforeonloadscriptTriggers before the document loadsonbeforeprintscriptTriggers before the document is printedOnblurscriptTriggers when the window loses focusOncanplayscriptTriggers when media can start play but might to ... Read More

Call JavaScript Function from onsubmit Event

usharani
Updated on 16-Jun-2020 11:37:14

2K+ Views

The onsubmit event occurs when you try to submit a form. You can put your form validation against this event type. The following example shows how to use onsubmit. Here, we are calling a validate() function before submitting a form data to the webserver. If validate() function returns true, the form will be submitted, otherwise it will not submit the data.     The HTML    .......    

Connectivity in a Directed Graph

Samual Sam
Updated on 16-Jun-2020 11:35:48

3K+ Views

To check connectivity of a graph, we will try to traverse all nodes using any traversal algorithm. After completing the traversal, if there is any node, which is not visited, then the graph is not connected.For the directed graph, we will start traversing from all nodes to check connectivity. Sometimes one edge can have the only outward edge but no inward edge, so that node will be unvisited from any other starting node.In this case, the traversal algorithm is recursive DFS traversal.Input and OutputInput: Adjacency matrix of a graph    0 1 0 0 0    0 0 1 0 ... Read More

Depth First Search (DFS) for a Graph

karthikeya Boyini
Updated on 16-Jun-2020 11:31:14

3K+ Views

The Depth-First Search (DFS) is a graph traversal algorithm. In this algorithm, one starting vertex is given, and when an adjacent vertex is found, it moves to that adjacent vertex first and tries to traverse in the same manner.It moves through the whole depth, as much as it can go, after that it backtracks to reach previous vertices to find the new path.To implement DFS in an iterative way, we need to use the stack data structure. If we want to do it recursively, external stacks are not needed, it can be done internal stacks for the recursion calls.Input and ... Read More

Advertisements