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
Front End Technology Articles
Page 329 of 652
How to call a function in JavaScript?
JavaScript allows us to write our own functions as well. To invoke a function somewhere later in the script, you would simply need to write the name of that function. Function Declaration Before calling a function, you need to declare it. Here's the basic syntax: function functionName() { // Code to execute } Calling a Function Once declared, you can call a function by writing its name followed by parentheses: function sayHello() ...
Read MoreHow to write JavaScript Regular Expression for multiple matches?
To find multiple matches in JavaScript, use the global flag (g) with regular expressions. The exec() method can be called repeatedly to find all matches in a string. Syntax let regex = /pattern/g; let matches; while ((matches = regex.exec(string)) !== null) { // Process each match } Example: Extracting URL Parameters This example extracts all demo parameters from a URL query string: var url = 'https://www.example.com/new.html?ui=7&demo=one&demo=two&demo=three'; var a = document.createElement('a'); a.href = url; var demoRegex = /(?:^|[&;])demo=([^&;]+)/g; var matches; var demo = []; ...
Read MoreWhat is function overloading in JavaScript?
JavaScript does not support function overloading like other programming languages such as Java or C++. When you define multiple functions with the same name, JavaScript keeps only the last defined function. What Happens with Same Function Names Let's see what happens when we define multiple functions with the same name: function funcONE(x, y) { return x * y; } function funcONE(z) { return z; } // Only the last function definition is kept console.log(funcONE(5)); // prints 5 console.log(funcONE(5, 6)); // prints ...
Read MoreHow much should be a JavaScript Line Length?
JavaScript line length refers to how many characters should fit on a single line of code. Following proper line length guidelines makes your code more readable and maintainable. Recommended Line Length The standard practice is to keep JavaScript lines under 80 characters. Some modern style guides allow up to 100 or 120 characters, but 80 remains the most widely adopted standard for better readability across different editors and devices. Breaking Long Lines When a statement exceeds the character limit, break it after a comma or operator, not in the middle of a variable name or string. ...
Read MoreHow to set the shadow effect of a text with JavaScript?
The textShadow property in JavaScript allows you to add shadow effects to text elements. This property accepts values for horizontal offset, vertical offset, blur radius, and color. Syntax element.style.textShadow = "h-shadow v-shadow blur-radius color"; Parameters h-shadow: Horizontal offset of the shadow (required) v-shadow: Vertical offset of the shadow (required) blur-radius: Blur distance of the shadow (optional) color: Color of the shadow (optional) Example: Basic Text Shadow Add Text Shadow Remove Shadow ...
Read MoreHow to define global variable in a JavaScript function?
In JavaScript, you can define global variables either at global scope or from within functions using the window object. Here are the main approaches. Method 1: Declaring at Global Scope The most straightforward way is to declare variables outside any function at global scope: var myGlobalVariable = "I'm global!"; function display() { console.log(myGlobalVariable); // Accessible here } display(); console.log(myGlobalVariable); // Also accessible here ...
Read MoreWhy does the JavaScript need to start with ";"?
JavaScript uses semicolons (;) to separate statements and avoid potential issues with automatic semicolon insertion (ASI). Starting lines with semicolons is a defensive programming practice. The Problem Without Semicolons JavaScript automatically inserts semicolons at line breaks, but this can cause unexpected behavior when lines are concatenated: // Without defensive semicolon let result = getValue() (function() { console.log("This might not work as expected"); })(); function getValue() { return "Hello"; } JavaScript interprets this as calling getValue() with a function as an argument, which causes an error. ...
Read MoreSet how much the item will grow relative to the rest of JavaScript.
Use the flexGrow property to set how much the item will grow relative to the rest of the flexible items with JavaScript. Syntax element.style.flexGrow = "value"; Parameters The flexGrow property accepts a numeric value that defines the growth factor: 0 - Item won't grow (default) 1 - Item grows equally with other flex items 2 or higher - Item grows proportionally more than others Example You can try to run the following code to learn how to work with flexGrow ...
Read MoreHow to set the page-break behavior before an element with JavaScript?
Use the pageBreakBefore property in JavaScript to set the page-break behavior before an element. Use the always property to insert a page break before an element. Note − The changes would be visible while printing or viewing the print preview. Syntax element.style.pageBreakBefore = "value"; Property Values Value Description auto Default - automatic page break ...
Read MoreDoes use of anonymous functions affect performance?
Anonymous functions in JavaScript have minimal performance impact in modern engines. While they create new function objects each time, the difference is negligible unless used in tight loops or performance-critical code. What are Anonymous Functions? Anonymous functions are functions without a name identifier. They are typically assigned to variables or passed as arguments to other functions. var func = function() { console.log('This is anonymous'); } func(); This is anonymous Performance Comparison Here's a comparison between named and anonymous functions: // Named function ...
Read More