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
Web Development Articles
Page 433 of 801
Window 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 MoreExtract the value of the to a variable using JavaScript?
To extract the value of the element to a variable using JavaScript, use the textContent property to get the text content from any HTML or SVG text element. Syntax var textValue = document.getElementById("elementId").textContent; Example Extract SVG Text Value This is the JavaScript Tutorial ...
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 MoreHow can we separate the special characters in JavaScript?
In JavaScript, you can separate special characters from text using the match() method with regular expressions. This technique splits strings into arrays containing both word characters and special characters as separate elements. Syntax arrayName.flatMap(item => item.match(/\w+|\W+/g)); The regular expression /\w+|\W+/g works as follows: \w+ matches one or more word characters (letters, digits, underscore) | acts as OR operator \W+ matches one or more non-word characters (special characters, spaces) g flag ensures global matching throughout the string Example: Separating Special Characters from Names Let's separate special characters from an array of ...
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 can I cut a string after X characters in JavaScript?
To cut a string after X characters in JavaScript, you can use several methods. The most common approaches are substr(), substring(), and slice(). Using substr() Method The substr() method extracts a portion of a string, starting at a specified index and extending for a given number of characters. var myName = "JohnSmithMITUS"; console.log("The String = " + myName); var afterXCharacter = myName.substr(0, 9); console.log("After cutting the characters the string = " + afterXCharacter); The String = JohnSmithMITUS After cutting the characters the string = JohnSmith Using substring() Method The substring() ...
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 MoreAvoid Unexpected string concatenation in JavaScript?
JavaScript string concatenation can lead to unexpected results when mixing strings and numbers. Using template literals with backticks provides a cleaner, more predictable approach than traditional concatenation methods. The Problem with Traditional Concatenation When using the + operator, JavaScript may perform string concatenation instead of numeric addition: let name = "John"; let age = 25; let score = 10; // Unexpected string concatenation console.log("Age: " + age + score); // "Age: 2510" (not 35!) console.log(name + " is " + age + " years old"); Age: 2510 John is 25 years ...
Read More