Which Equals Operator to Use in JavaScript

Nancy Den
Updated on 07-Jan-2020 10:59:49

172 Views

Double equals (==) is abstract equality comparison operator, which transforms the operands to the same type before making the comparison. For example,5 ==  5       //true '5' == 5      //true 5 == '5'      //true 0 == false    //trueTriple equals (===) are strict equality comparison operator, which returns false for different types and different content.For example,5 === 5  // true 5 === '5' // false var v1 = {'value':'key'}; var v2 = {'value': 'key'}; v1 === v2 //false

Ternary Operator in JavaScript

Swarali Sree
Updated on 07-Jan-2020 10:58:35

609 Views

The conditional operator or ternary operator first evaluates an expression for a true or false value and then executes one of the two given statements depending upon the result of the evaluation.S.NoOperator & Description1? : (Conditional )If Condition is true? Then value X: Otherwise value YExampleYou can try to run the following code to understand how Ternary Operator works in JavaScriptLive Demo                    var a = 10;          var b = 20;          var linebreak = "";          document.write ("((a > b) ? 100 : 200) => ");          result = (a > b) ? 100 : 200;          document.write(result);          document.write(linebreak);          document.write ("((a < b) ? 100 : 200) => ");          result = (a < b) ? 100 : 200;          document.write(result);          document.write(linebreak);          

Determine If an Argument is Sent to a JavaScript Function

Krantik Chavan
Updated on 07-Jan-2020 10:29:12

119 Views

To determine if an argument is sent to function, use “default” parameters.ExampleYou can try to run the following to implement itLive Demo                    function display(arg1 = 'Tutorials', arg2 = 'Learning') {             document.write(arg1 + ' ' +arg2+"");          }          display('Tutorials', 'Learning');          display('Tutorials');          display();          

Finger Searching in Data Structure

Arnab Chakraborty
Updated on 07-Jan-2020 09:42:30

438 Views

A finger search on a data structure is defined as an extension of any search operation that structure supports, where a reference (finger) to an element in the data structure is given along with the query. While the search time for an element is most frequently denoted as a function of the number of elements in a data structure, finger search times are treated as a function of the distance between the element and the finger.In a set of m elements, the distance d(a, b) between two elements a and b is their difference in rank. If elements a and ... Read More

Use Variable Number of Arguments in JavaScript Function

Krantik Chavan
Updated on 07-Jan-2020 09:37:34

650 Views

To use a variable number of arguments to function, use the arguments object.ExampleYou can try to run the following code to implement arguments to a function in JavaScriptLive Demo                    function functionArgument(val1, val2, val3)  {             var res = "";             res += "Expected Arguments: " + functionArgument.length;             res += "";             res += "Current Arguments : " + arguments.length;             res += "";             res += "Each argument = "             for (p = 0; p < arguments.length; p++) {                res += "";                res += functionArgument.arguments[p];                res += " ";             }             document.write(res);          }          functionArgument(20, 50, 80, "Demo Text!","Hello World", new Date())          

JavaScript Version of Sleep Function

Alankritha Ammu
Updated on 07-Jan-2020 09:35:37

414 Views

The JavaScript version of sleep() is “await”. The await feature pauses the current aync function.ExampleYou can try to run the following code to implement sleep in JavaScriptLive Demo                    function setSleep(ms) {             return new Promise(resolve => setTimeout(resolve, ms));          }          async function Display() {             document.write('Wait for 5 seconds!');             await setSleep(5000);             document.write('After 5 seconds!');          }          Display();          

Use of Return Statement Inside a Function in JavaScript

Alankritha Ammu
Updated on 07-Jan-2020 08:51:24

315 Views

The “return” statement in JavaScript mentions a value, which you like to return. Inside a function, the value is returned to the function caller.ExampleYou can try to run the following code to implement return statement inside a functionLive Demo                    function multiply(num1, num2) {             return num1 * num2;          }          var result = multiply(5, 10);          document.write("Multiplication: "+result);          

Define a Method in JavaScript

seetha
Updated on 07-Jan-2020 08:07:26

465 Views

A method in JavaScript is the action performed on objects. A JavaScript method has a function definition, which is stored as a property value. ExampleLet’s see an example to define a method in JavaScriptLive Demo                                 var department = {                deptName: "Marketing",                deptID : 101,                deptZone : "North",                details : function() {                   return "Department Details" + "Name: " + this.deptName + " Zone: " + this.deptZone + "ID: " + this.deptID;                }             };          document.getElementById("myDept").innerHTML = department.details();          

Define Functions Inside a Function Body in JavaScript

mkotla
Updated on 07-Jan-2020 08:06:24

196 Views

To achieve what you want, use JavaScript Closures. A closure is a function, which uses the scope in which it was declared when invoked. It is not the scope in which it was invoked.ExampleLet’s take your example and this is how you can achieve your task. Here, innerDisplay() is a JavaScript closure.Var myFunction = (function () {    function display() {       // 5    };    function innerDisplay (a) {       if (/* some condition */ ) {          // 1          // 2          display();       }else {          // 3          // 4          display();       }    }    return innerDisplay; })();

Use Arrow Functions as Methods in JavaScript

Swarali Sree
Updated on 07-Jan-2020 08:03:40

217 Views

Fat arrow function as the name suggests helps in decreasing line of code. The syntax => shows fat arrow. This also avoids you to write the keyword “function” repeatedly. Arrow functions are generally used for a non-method function. Let’s see how to use arrow functions used as methods:ExampleYou can try to run the following code to implement arrow functions used as methodsLive Demo                    'use strict';          var ob1 = {             val1: 75,             val2: 100,             x: () => document.write(this.val1, this),             y: function() {                document.write(""+this.val1, this);             },             z: function() {                document.write(""+this.val2, this);             },          }          ob1.x();          ob1.y();          ob1.z();          

Advertisements