Found 6710 Articles for Javascript

Get div height with vanilla JavaScript

Abdul Rawoof
Updated on 02-Sep-2022 11:58:51

13K+ Views

The HTML element is represented by the Div Object in the HTML DOM. When styling other HTML elements with CSS or with JavaScript, this tag is used to indicate the container for those elements. The default container for flow content in HTML is the div element. It doesn't affect the content or layout until it is CSS-styled in some way. The height of the div element can be known in many ways. These ways are discussed in this article. Using offsetHeight The offsetHeight is a property which returns the height of the element in pixels. Syntax The syntax of ... Read More

How to create a new object with only a subset of properties using vanilla Javascript

Ayush Gupta
Updated on 02-Dec-2019 05:54:32

265 Views

To get a subset of object's properties and create a new object out of those properties, use object destructuring and property shorthand. For example, You have the following object −Exampleconst person = {    name: 'John',    age: 40,    city: 'LA',    school: 'High School' }And you only want the name and age, you can create the new objects using −const {name, age} = person; const selectedObj = {name, age}; console.log(selectedObj);OutputThis will give the output −{    name: 'John',    age: 40 }

How to get a subset of a javascript object's properties?

Ayush Gupta
Updated on 02-Dec-2019 05:53:16

311 Views

To get a subset of object's properties and create a new object out of those properties, use object destructuring and property shorthand. For example, You have the following object −Exampleconst person = {    name: 'John',    age: 40,    city: 'LA',    school: 'High School' }And you only want the name and age, you can create the new objects using −const {name, age} = person; const selectedObj = {name, age}; console.log(selectedObj);OutputThis will give the output −{    name: 'John',    age: 40 }

What blocks Ruby, Python to get Javascript V8 speed?

Ayush Gupta
Updated on 02-Dec-2019 05:51:42

130 Views

Nothing. They can get to V8 speed if proper investments are made in optimizing those language engines as are made for JS by Google in the V8 project.This is all really a matter of how much push is provided to the language by sponsoring organizations to further the development and optimization effort on these languages.

Converting strings to numbers with vanilla JavaScript

Ayush Gupta
Updated on 02-Dec-2019 05:41:54

205 Views

The parseInt function available in JavaScript has the following signature −SyntaxparseInt(string, radix);Where the parameters are the following −string − The value to parse. If this argument is not a string, then it is converted to one using the ToString method. Leading whitespace in this argument is ignored.radix − An integer between 2 and 36 that represents the radix (the base in mathematical numeral systems) of the string.So we can pass the string and the radix and convert any number with base from 2 to 36 to integer using this method. For example, Exampleconsole.log(parseInt("100", 10)) console.log(parseInt("10", 8)) console.log(parseInt("101", 2)) console.log(parseInt("2FF3", 16)) ... Read More

Jasmine JavaScript Testing - toBe vs toEqual

Ayush Gupta
Updated on 02-Dec-2019 05:39:23

805 Views

Arrays can be compared in 2 ways −They refer to the same array object in memory.They may refer to different objects but their contents are all equal.ExampleFor case 1, jasmine provides the toBe method. This checks for reference. For example, describe("Array Equality", () => {    it("should check for array reference equility", () => {       let arr = [1, 2, 3];       let arr2 = arr       // Runs successfully       expect(arr).toBe(arr2);       // Fails as references are not equal       expect(arr).toBe([1, 2, 3]);    }); });OutputThis ... Read More

If a DOM Element is removed, are its listeners also removed from memory in javascript?

Ayush Gupta
Updated on 02-Dec-2019 05:35:14

3K+ Views

In modern browsers, if a DOM Element is removed, its listeners are also removed from memory in javascript.Note that this will happen ONLY if the element is reference-free. Or in other words, it doesn't have any reference and can be garbage collected. Only then its event listeners will be removed from memory.

What is the difference between 'throw new Error' and 'throw someObject' in javascript?

Ayush Gupta
Updated on 02-Dec-2019 05:34:13

550 Views

The difference between 'throw new Error' and 'throw someObject' in javascript is that throw new Error wraps the error passed to it in the following format −{    name: 'Error',    message: 'Whatever you pass in the constructor' }The throw someObject will throw the object as is and will not allow any further code execution from the try block, ie same as throw new Error.

How to remove the hash from window.location (URL) with JavaScript without page refresh?

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

What are the differences between Deferreds, Promises and Futures in javascript?

Ayush Gupta
Updated on 02-Dec-2019 05:31:52

1K+ Views

Future is an old term that is same as promise.A promise represents a value that is not yet known. This can better be understood as a proxy for a value not necessarily known when the promise is created.A deferred represents work that is not yet finished. A deferred (which generally extends Promise) can resolve itself, while a promise might not be able to do so. This can also be thought of as a promise that'll always succeed only.A promise is a placeholder for a result which is initially unknown while a deferred represents the computation that results in the value.Read More

Advertisements