Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Javascript Articles
Page 431 of 534
Creating JavaScript constructor using the "new" operator?
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 MoreReturning values from a constructor in JavaScript?
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 MoreExplain the finally Statement in JavaScript with examples.
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 MoreWhat is JavaScript type coercion?
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 MoreWindow innerWidth and innerHeight Properties in JavaScript.
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 MoreHighlight a text, every time page loads with JavaScript
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 MoreHow to invoke a function as a function and method?
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 MoreBlock Scoping in JavaScript.
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 MoreExplain JavaScript Error Object.
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 MoreHow to remove elements using the splice() method in JavaScript?
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