Found 10711 Articles for Web Development

How to use arrow functions used as methods in JavaScript?

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

141 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();          

What is a fat arrow function in JavaScript?

Samual Sam
Updated on 13-Sep-2019 08:40:44

536 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.Here’s the syntax:argument => expressionUse the following for more than one argument:(argument1 [, argument2]) => expressionLet’s compare a function with and without fat arrow:Function in JavaScriptvar rank = [7,8,9]; var display = rank.map(function(num) {    return num * num; });Fat Arrow function in JavaScriptvar rank= [7,8,9]; var display = rank.map((num) => num*num); document.write(arr)Arrow function definitely reduces the code lines.

Why avoid increment (“++”) and decrement (“--”) operators in JavaScript?

Rahul Sharma
Updated on 15-Jun-2020 05:41:11

382 Views

The increment and decrement operators should be avoided since it can lead to unexpected results. Here are some of the conditions:In an assignment statement, it can lead to unfavorable results:ExampleLive Demo                    var a = 5;          var b = ++a; var c = a++;          var d = ++c;          document.write(a);          document.write("\r"+b);          document.write("\r"+c);          document.write("\r"+d);           OutputWhitespace between the operator and variable can also lead to unexpected results:a = b = c = 1; ++a ; b -- ; c;

What is decrement (--) operator in JavaScript?

Johar Ali
Updated on 15-Jun-2020 05:39:59

443 Views

The decrement operator decreases an integer value by one. Here’s an example where the value of a is decremented twice using decrement operator twice −ExampleLive Demo                    

What is increment (++) operator in JavaScript?

Amit Sharma
Updated on 15-Jun-2020 05:39:26

629 Views

The increment operator increases an integer value by one. Here’s an example where the value of a is incremented twice using the increment operator twiceExampleLive Demo                    var a = 33;          a = ++a;          document.write("++a = ");          result = ++a;          document.write(result);          

Can I declare JavaScript variables as specific types?

Shubham Vora
Updated on 22-Aug-2022 08:58:59

2K+ Views

In this tutorial, we will see if we can declare JavaScript variables as a specific type or not. The JavaScript contains the three reserve keywords to declare the variables: ‘let, ’ ‘var’, and ‘const.’ When the user declares the variable using any keyword, it has a different scope. The variable declared with the const keyword always remains constant, and we can’t change its value after initializing it. The variables declared with the let keyword have block scope and can’t be accessed outside its scope. When we declare the variables with the var keyword, it can have the global scope or ... Read More

How do I empty an array in JavaScript?

Shubham Vora
Updated on 20-Jul-2022 08:40:09

375 Views

This tutorial teaches us to make an array empty in JavaScript. While programming with JavaScript, programmers need to make an array empty in many conditions. For example, coders are doing competitive programming with JavaScript. Suppose, to solve some problem, they need to create a new or an empty array while calling the function every time. Users can visualize this problem from the below example.function child ( ){    // create a new array or use a single array and make it empty every time function invokes    }    function parent ( ) {       for ( int ... Read More

How can I check whether a variable is defined in JavaScript?

Shubham Vora
Updated on 12-Jul-2022 13:58:45

3K+ Views

In this tutorial, we will learn to check the existence of the variable, which means whether the variable is declared and initialized or not in JavaScript. In many scenarios, JavaScript programmers need to check the presence of the variable. Otherwise, it throws a reference error if we use it without declaring or defining it.For example, Programmers have defined the variable and it is uninitialized. Programmers want to change the variables' value through the data they will get from API calls. Suppose that the value of the uninitialized variable is not updated due to some error during the API call. If ... Read More

How to check a not-defined variable in JavaScript?

Rahul Sharma
Updated on 15-Jun-2020 05:26:32

159 Views

To check a variable is ‘undefined’, you need to check using the following. If the result is “false”, it means the variable is not defined. Here, the variable results in “True” −ExampleLive Demo                    var points = 100;             if(points){                document.write("True");             }else{                document.write("False");             }          

What is the difference between: var functionName = function() {} and function functionName() {} in Javascript

Johar Ali
Updated on 15-Jun-2020 05:25:58

280 Views

functionDisplayOne is a function expression, however, functionDisplayTwo is a function declaration. It is defined as soon as its surrounding function is executed.Both the ways are used to declare functions in JavaScript and functionDisplayOne is an anonymous function.Here’s the function expression −functionDisplayOne(); var functionDisplayOne = function() {    console.log("Hello!"); };The following is the function declaration −functionDisplayTwo(); function functionDisplayTwo() {    console.log("Hello!"); }

Advertisements