Javascript Articles

Page 431 of 534

Creating JavaScript constructor using the "new" operator?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 199 Views

In JavaScript, constructor functions create objects using the new operator. A constructor function is a regular function that becomes a template for creating multiple objects with similar properties. Syntax function ConstructorName(param1, param2) { this.property1 = param1; this.property2 = param2; } // Create new object let obj = new ConstructorName(value1, value2); Example JavaScript Constructor body ...

Read More

Returning values from a constructor in JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 226 Views

In JavaScript, constructors typically don't need explicit return statements since they automatically return the newly created object. However, you can override this behavior by explicitly returning an object from a constructor. How Constructor Returns Work When you use the new keyword with a constructor: If the constructor returns an object, that object becomes the result If the constructor returns a primitive value (string, number, boolean), it's ignored and this is returned instead If there's no explicit return, this is returned automatically Example: Returning an Object from Constructor ...

Read More

Explain the finally Statement in JavaScript with examples.

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 240 Views

The finally statement in JavaScript always executes after try and catch blocks, regardless of whether an error occurred or not. This makes it ideal for cleanup operations and code that must run in all scenarios. Syntax try { // Code that may throw an error } catch (error) { // Handle error (optional) } finally { // Always executes } Example: Finally Block Always Executes ...

Read More

What is JavaScript type coercion?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 278 Views

Type coercion in JavaScript refers to the automatic conversion of values from one data type to another. This happens implicitly when JavaScript needs to perform operations between different types. How Type Coercion Works JavaScript automatically converts types in certain situations, such as when using operators or comparing values. This can sometimes lead to unexpected results if you're not familiar with the coercion rules. String Coercion Example Type Coercion Example JavaScript Type Coercion ...

Read More

Window innerWidth and innerHeight Properties in JavaScript.

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 242 Views

The window.innerWidth and window.innerHeight properties return the width and height of the browser window's content area, excluding toolbars, scrollbars, and borders. Syntax let width = window.innerWidth; let height = window.innerHeight; Properties innerWidth - Returns the interior width of the window in pixels innerHeight - Returns the interior height of the window in pixels Both properties are read-only and return integer values Values change when the browser window is resized Example Window Dimensions ...

Read More

Highlight a text, every time page loads with JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 353 Views

To highlight text every time a page loads in JavaScript, you can use a for loop to iterate through words and wrap specific words in elements with CSS classes for styling. Approach The technique involves: Getting the text content using getElementById() Splitting the text into an array of words using split(" ") Looping through each word to find matches Wrapping matching words with tags Rejoining the array and updating the HTML content Example Text ...

Read More

How to invoke a function as a function and method?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 173 Views

In JavaScript, functions can be invoked in different ways. When called directly, it's function invocation. When called as a property of an object, it's method invocation. Function vs Method Invocation Function invocation: Calling a function directly by its name. Method invocation: Calling a function that belongs to an object using dot notation. Example Function and Method Invocation body { ...

Read More

Block Scoping in JavaScript.

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 353 Views

Block scope is an area between two curly braces { } which can be between loops, if conditions, or switch statements. The let and const keywords introduced in ES2015 allow us to create block-scoped variables that can only be accessed within that specific block. Understanding Block Scope Variables declared with let and const have block scope, meaning they exist only within the nearest enclosing block. This is different from var, which has function scope. Example: Block Scope with let Block Scoping Example ...

Read More

Explain JavaScript Error Object.

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 366 Views

The Error object is thrown by the JavaScript interpreter when a script error occurs. It can also be used to create custom exceptions. Understanding Error objects helps in debugging and error handling in JavaScript applications. Error Object Properties Property Description name Sets or returns the name/type of the error message Sets or returns the error message as a string Example: Catching and Displaying Error Properties JavaScript Error Object body { ...

Read More

How to remove elements using the splice() method in JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 358 Views

The splice() method is used to remove elements from an array by specifying the starting index and the number of elements to delete. It modifies the original array and returns an array of the removed elements. Syntax array.splice(startIndex, deleteCount) Parameters startIndex: The index at which to start removing elements deleteCount: The number of elements to remove Example: Remove Single Element Splice Method Example Remove Elements using ...

Read More
Showing 4301–4310 of 5,340 articles
« Prev 1 429 430 431 432 433 534 Next »
Advertisements