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 509 of 534
What is the difference between single and double quotes in JavaScript?
In JavaScript, you can use either single quotes or double quotes to define strings. Both are functionally identical, but it's important to maintain consistency throughout your code. Syntax let singleQuoted = 'Hello World'; let doubleQuoted = "Hello World"; Basic Examples let message1 = "Hello, JavaScript!"; let message2 = 'Hello, JavaScript!'; console.log(message1); console.log(message2); console.log(message1 === message2); // Both are identical Hello, JavaScript! Hello, JavaScript! true Escaping Quotes When your string contains quotes, you need to escape them or use the opposite quote type: // Escaping ...
Read MoreHow to write a JavaScript function to get the difference between two numbers?
Use Math.abs() inside a JavaScript function to get the difference between two numbers in JavaScript. The Math.abs() method returns the absolute value, ensuring you always get a positive difference regardless of which number is larger. Syntax function getDifference(num1, num2) { return Math.abs(num1 - num2); } Example: Using Math.abs() for Difference You can try to run the following code to get the difference of numbers: var num1, num2; ...
Read MoreWhat does the operator || do in a var statement in JavaScript?
In JavaScript, the logical OR operator (||) is commonly used in variable declarations to provide fallback or default values. When used with var, it returns the first "truthy" value or the last value if all are falsy. The logical OR operator evaluates operands from left to right and returns the first truthy value it encounters. If no truthy value is found, it returns the last operand. In JavaScript, falsy values include false, 0, "", null, undefined, and NaN. Syntax var result = expr1 || expr2; If expr1 is truthy, result will be expr1. Otherwise, ...
Read MoreHow to extract the hostname portion of a URL in JavaScript?
In this tutorial, we will see how to extract the hostname portion of a URL in JavaScript. What is a URL? A URL (Uniform Resource Locator) is a web address that identifies a specific resource on the internet. For example, tutorialspoint.com is a word-based URL. An IP address can also be used as a URL (ex. 192.168.2.24). Since names are simpler to recall than numbers, most users submit the name's address when searching on the internet. A URL is a method by which web browsers request specific pages from web servers. The syntax/format of a URL is ...
Read MoreIs it required to have a return a value from a JavaScript function?
In JavaScript, functions do not require a return statement. The return statement is optional, and functions can operate without explicitly returning a value. Functions Without Return Values When a function doesn't have a return statement, it automatically returns undefined: function greetUser(name) { console.log("Hello, " + name + "!"); // No return statement } let result = greetUser("Alice"); console.log("Function returned:", result); Hello, Alice! Function returned: undefined Functions With Return Values Functions can explicitly return values using the return statement: function ...
Read MoreWhat is a standard for commenting a function in JavaScript?
JavaScript is used everywhere, from creating the back end using environments like Node.js to creating the front end using React.js, Vue.js, etc. In JavaScript, functions are fundamental building blocks used for operations, callbacks, constructors, and many other purposes. With extensive function usage, code can become messy and hard to debug. It becomes difficult to track which functions trigger which events, making proper commenting essential for maintainability. Writing effective function comments requires following standards that other developers can easily understand and work with. Standards for Commenting Functions in JavaScript Following are essential standards for commenting functions: ...
Read MoreHow to call a parent window function from an iframe using JavaScript?
When working with iframes, you often need to call functions defined in the parent window from within the iframe. JavaScript provides several methods to achieve this cross-frame communication. Using window.parent The most common approach is using window.parent to reference the parent window: Parent Window Parent Window function displayMessage(message) { ...
Read MoreHow to use unlimited arguments in a JavaScript function?
In JavaScript, functions typically accept a fixed number of parameters, but there are several ways to handle an unlimited number of arguments. This tutorial explores three methods to pass and process multiple arguments in JavaScript functions. Using ES5 Arguments Object The arguments object is available in all non-arrow functions and contains all arguments passed to the function, regardless of how many parameters are defined. Syntax function functionName(param1, param2) { var actualParams = functionName.length; // Number of defined parameters var totalArgs = arguments.length; ...
Read MoreHow can I declare optional function parameters in JavaScript?
This tutorial will teach us how to declare optional function parameters in JavaScript. While declaring the function, we pass some variables to the function definition to use it inside the function block, called function parameters. The function parameters can also be optional, which gives us the independence to pass function arguments when we call the function. Here, Arguments are the values we pass while calling the function, and parameters are the variables we pass in the function definition. What Are Optional Parameters? The optional parameter word means that you don't need to pass that parameter every time ...
Read MoreWhat is Multiplication Assignment Operator (*=) in JavaScript?
The multiplication assignment operator (*=) in JavaScript provides a shorthand way to multiply a variable by a value and assign the result back to the variable. Instead of writing a = a * b, you can simply use a *= b. Syntax operand1 *= operand2 // equivalent to: operand1 = operand1 * operand2 The multiplication assignment operator is a binary operator that multiplies the left operand by the right operand and stores the result in the left operand. Example 1: Basic Usage with Numbers Here's how the multiplication assignment operator works with numeric ...
Read More