JavaScript Variables: Declare Outside or Inside Loop

Abdul Rawoof
Updated on 26-Aug-2022 11:56:41

5K+ Views

It is believed that there can be any performance-based differences when the variables are declared inside or outside the loops, but there will be no difference. As there is flexibility in any programming language to declare the variables as and when required which will be easy for the users to read and understand accordingly. So, in JavaScript too the variables can be declared inside or outside the loop as per the users ease anywhere in the function body. And even variable declarations are not commands that are executed at the run-time. If the variable is declared outside the loop, then ... Read More

String Interpolation in JavaScript

Abdul Rawoof
Updated on 26-Aug-2022 11:56:24

18K+ Views

String interpolation in JavaScript is a process in which an expression is inserted or placed in the string. To insert or embed this expression into the string a template literal is used. By using string interpolation in JavaScript, values like variables and mathematical expressions and calculations can also be added. The template literal has a dollar sign followed by curly brackets. Inside the curly brackets of this template literal, the expression or the mathematical calculation which is to evaluated and embedded should be written. Syntax The syntax of the string interpolation in JavaScript is as follows. `string where interpolation should ... Read More

Difference Between Window Location and Document Location

Abdul Rawoof
Updated on 26-Aug-2022 11:56:00

2K+ Views

The window.location property The location property of a window (i.e. window.location) is a reference to a Location object; it represents the current URL of the document being displayed in that window. Since window object is at the top of the scope chain, so properties of the window.location object can be accessed without the window prefix. For example window.location.href can be written as location.href. The following section will show you how to get the URL of page as well as hostname, protocol, etc. using the location object property of the window object. You can use the window.location.href property to get the ... Read More

Add Class to DOM Element in JavaScript

Abdul Rawoof
Updated on 26-Aug-2022 11:55:32

1K+ Views

HTML is a mark-up language used to create the useful elements of a webpage. The DOM which means Document Object Model is used to manipulate the HTML Document. In DOM, all the elements are defined as objects. The styling in the HTML document can be done by using CSS. In here, JavaScript is being used to manipulate them. The ways in which the class can be added to a class to the DOM elements are described below. Using className If the element has a class already, then it will just add another class to it if not the element gets ... Read More

Typecasting in JavaScript

Abdul Rawoof
Updated on 26-Aug-2022 11:55:13

21K+ Views

Typecasting in JavaScript means converting one data type to another data type i.e., the conversion of a string data type to Boolean or the conversion of an integer data type to string data type. The typecasting in JavaScript is also known as type conversion or type coercion. Types of conversions The type casting is of two types in JavaScript. They are the implicit type conversions and the explicit type conversions. Implicit type casting The implicit type casting is the conversion of data type done due to the internal requirement or automatic conversion by the compiler or the interpreter. To understand ... Read More

Get the Primitive Value of String in JavaScript

Abdul Rawoof
Updated on 26-Aug-2022 11:54:55

1K+ Views

In JavaScript if the data is not an object and has no methods and properties is called as a primitive data type or a primitive value. The different types of primitive data types in JavaScript are string, number, Boolean, undefined, symbol, null and big int. The primitive types boolean, string and number can be wrapped by their wrapper object, i.e., instances of the Boolean, String and Number constructors respectively. To get the back the primitive values back from the object wrappers, we need to call the valueOf() method on the objects. Example Following example demonstrates the primitive and object types ... Read More

Check for Null, Undefined or Blank Variables in JavaScript

Abdul Rawoof
Updated on 26-Aug-2022 11:54:34

587 Views

No there is not a standard function to check for null, undefined or blank values in JavaScript. However, there is the concept of truthy and falsy values in JavaScript. Values that coerce to true in conditional statements are called truth values. Those that resolve to false are called falsy. According to ES specification, the following values will evaluate to false in a conditional context − null undefined NaN empty string ("") 0 false This means that none of the following if statements will get executed − if (null) if (undefined) if ... Read More

Remove Hash from Window Location URL with JavaScript

Abdul Rawoof
Updated on 26-Aug-2022 11:54:02

3K+ Views

The replaceState() method replaces the current history entry with the state objects, title, and URL passed as method parameters. This method is useful when you want to update the current history entry's state object or URL in response to a user action. To remove the hash URL, use the history API's replaceState method to remove the hash location. Syntax The syntax for the replaceState() method is as follows − window.history.replaceState(stateObj, "unused", "url") The function will take three parameters. They are − stateObj − This parameter is associated with history entry which is passed to the replace method. This ... Read More

Convert Image to Data URI with JavaScript

Abdul Rawoof
Updated on 26-Aug-2022 11:53:41

12K+ Views

JavaScript has a convention for converting an image URL or a local PC image to a base64 string. This string can contain a variety of symbols and letters. In here it is explained how to make a canvas element, load an image into it, and use toDataURL() to display the string representation. To obtain the base64 string representation, we will also use the file reader option. In this case, a canvas element will be created and its dimensions will be specified. The dataURL where the string representation will be stored. We will add random images from online sources and ensure ... Read More

Convert Images to Base64 Data URL Using JavaScript

Abdul Rawoof
Updated on 26-Aug-2022 11:53:01

12K+ Views

JavaScript has a convention for converting an image URL or a local PC image to a base64 string. This string can contain a variety of symbols and letters. In here it is explained how to make a canvas element, load an image into it, and use toDataURL to display the string representation. To obtain the base64 string representation, we will also use the file reader option. In this case, it is created as a canvas element and its dimensions will be specified. The dataURL where the string representation will be stored. We will add random images from online sources and ... Read More

Advertisements