Found 6685 Articles for Javascript

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!"); }

How to check for “undefined” variable in JavaScript?

Samual Sam
Updated on 30-Jul-2019 22:30:21

154 Views

On getting the result of the following, you can find whether a variable is ‘undefined’. If the result is “false”, it means the variable is undefined. Here, the variable results in “True” Example Live Demo var res = 10; if(res) { document.write("True"); } else { document.write("False"); }

How to generate random whole numbers in JavaScript in a specific range?

Amit Sharma
Updated on 15-Jun-2020 05:25:14

175 Views

To generate a random number, use the JavaScript Math.random() method. Here set the minimum and maximum value and generate a random number between them as in the following code −ExampleLive Demo                    var max = 6          var min = 2          var random = Math.floor(Math.random() * (max - min + 1)) + min;          document.write(random)          

How to pass JavaScript Variables with AJAX calls?

Shubham Vora
Updated on 15-Sep-2022 11:53:13

8K+ Views

In this tutorial, we will learn how to pass JavaScript Variables with AJAX calls. AJAX stands for Asynchronous JavaScript and XML. As the name suggests, it performs asynchronous operations on your web page. AJAX communicates with the server by HTTP requests and gets the required data as an HTTP response. We can control these HTTP requests by passing the JavaScript variables with AJAX calls. There are various types of HTTP requests, and in this tutorial, we will discuss the most popular HTTP requests by using AJAX and also pass JavaScript variables with it. Pass JavaScript Variables with “GET” AJAX calls ... Read More

Where are JavaScript variables stored?

Samual Sam
Updated on 07-Jan-2020 06:58:27

1K+ Views

Like many other programming languages, JavaScript has variables. Variables can be thought of as named containers. You can place data into these containers and then refer to the data simply by naming the container.JavaScript variables get stored in the memory of the browser process. The following ways can work for storing variables:The variables which you declare in your JavaScript code gets saved in the memory of the browser process.Cookies can also store variables, they are often saved on your hard disk;

What is the best way of declaring multiple Variables in JavaScript?

Amit Sharma
Updated on 13-Sep-2019 07:37:26

125 Views

Definitely, the following way of declaring multiple variables is more efficient:var variable1 = 5; var variable2 = 3.6; var variable3 = “Amit”;Suppose you need to add, remove, or update a variable, then you can easily do it.But with the following method, you need to do more changes. For example, on removing a variable, you need to remove the semi-colon. If it’s the first, then you need to add var to the second variable.var variable1 = 5, variable2 = 3.6, variable3 = “Amit”

Advertisements