Found 6683 Articles for Javascript

Apply an IF condition to every element in an HTML table with JavaScript?

Yaswanth Varma
Updated on 21-Apr-2023 16:28:44

4K+ Views

The HTML tables are created using the tag in which the tag is used to create table rows and tag is used to create data cells. The elements under are regular and left aligned by default. The HTML tables allow web authors to arrange data like text, images, links, other tables, etc. into rows and columns of cells. Let’s dive into the article to learn more about applying an if condition to every element in an HTML table. Applying if condition to every element in an HTML table If a certain condition is true, the if/else ... Read More

I'm trying to make an id searcher which does a thing when you input the right id. However, the JavaScript if statement always runs. How?

AmitDiwan
Updated on 27-Oct-2020 09:36:16

43 Views

If you will use equal operator(=) in if condition the if block will always be executed.You need to use == operator or === .ExampleFollowing is the code −var details = [    {       id: 10001,       name: "John"    },    {       id: 10002,       name: "Bob"    },    {       id: 10003,       name: "Carol"    },    {       id: 10004,       name: "David"    } ] var searchId = 10003; for (var index = 0; index < details.length; index++) {    if (details[index].id === searchId) {       console.log(details[index].id + " found");       break;    } }To run the above program, you need to use the below command −node fileName.js.Here, my file name is demo322.js.OutputThis will produce the following output −PS C:\Users\Amit\javascript-code> node demo322.js 10003 found

Return correct value from recursive indexOf in JavaScript?

Yaswanth Varma
Updated on 21-Apr-2023 16:36:35

277 Views

A computer pattern or idea called recursion is present in many programming languages, including JavaScript. It is a feature used to build a function that repeatedly calls itself, but with a smaller input each time, until the intended outcome of the code is realized. In this article, we will return the correct value from a recursive indexof() function in JavaScript. Let’s dive into the article for a better understanding. Accurate value from the recursive indexof() function The position of the first occurrence of a value in a string is returned by the indexOf() method. -1 is returned by the indexOf() ... Read More

ES6/ECMA6 template literals not working in JavaScript?

Yaswanth Varma
Updated on 18-Jan-2023 11:48:00

1K+ Views

Template literals are a powerful and useful feature of the ECMAScript 6 (ES6) JavaScript language. However, there may be times when template literals do not work properly in your code. This article will discuss some common issues you may encounter with template literals, as well as potential solutions for resolving them. Additionally, we'll provide an overview of what template literals are and how they can be used to create more efficient and readable code. Template literals were known as template strings prior to ES6. Template literals are surrounded by the backtick (' ') character as opposed to quotes in ... Read More

JavaScript regex - How to replace special characters?

Yaswanth Varma
Updated on 21-Jun-2024 15:04:35

9K+ Views

Characters that are neither alphabetical nor numeric are known as special characters. Special characters are essentially all unreadable characters, including punctuation, accent, and symbol marks. Remove any special characters from the string to make it easier to read and understand. Before we jump into the article let’s have a quick view on the regex expression in JavaScript. Regex expression in JavaScript Regular expressions are patterns that can be used to match character combinations in strings. Regular expressions are objects in JavaScript. These patterns can be used with the exec() and test() methods of RegExp as well as the match(), matchAll(), ... Read More

Get the number of true/false values in an array using JavaScript?

AmitDiwan
Updated on 26-Oct-2020 11:25:52

1K+ Views

To get the number of true/ false values in an array, the code is as follows −var obj = [    {       isMarried: true    },    {       isMarried: false    },    {       isMarried: true    },    {       isMarried: true    },    {       isMarried: false    } ] function numberOfTrueValues(obj) {    var counter = 0;    for (var index = 0; index < obj.length; index++) {       if (obj[index].isMarried === true) {          counter++;     ... Read More

Is it possible to change the value of the function parameter in JavaScript?

Yaswanth Varma
Updated on 18-Jan-2023 11:04:48

4K+ Views

In this article we are going to find out whetehr it is possibleto change the value of the function parameter in JavaScript. A function accepts a certain values as parameters an later while calling this function we need to pass values for these parameters as arguments. Typically a function definition contains the name of the function, parameters (optional) and the body of the function.Syntax Following is the syntax for function parameter − function Name(paramet1, paramet2, paramet3, paramet4) { // Statements } Yes, it is possible to change the value of the function parameter in JavaScript. ... Read More

Set object property in an array true/false, whether the id matches with any id from another array of objects in JavaScript?

Yaswanth Varma
Updated on 18-Jan-2023 11:09:37

582 Views

The task we are going to perform in this article is ‘set object property in an array true/false whether the id matches with any if from another array of objects in JavaScript’. Before we jump into the article let’s have a glance at the object and array in JavaScript. An object in JavaScript is a separate entity having properties and a type. JavaScript objects have properties that specify their attributes. Multiple values can be kept in a single variable by using arrays. Compare that to a variable that only has room for one value. Each item in an array has ... Read More

Add class name in different li by pure in JavaScript?

Yaswanth Varma
Updated on 18-Jan-2023 11:36:56

2K+ Views

A class serves as a model for building objects. Code is used to encapsulate data, so that it can be worked on. Although JS classes are based on prototypes, they also have some unique syntax and semantics that are not shared by ES5 classes. Let’s dive into the article to learn more about how to add class name in different li by pure in JavaScript. To add class, use forEach() along with classList.add(). The forEach() in JavaScript For each entry in an array, the forEach() method invokes a different function. When dealing with empty elements, this method is not ... Read More

Understanding the find() method to search for a specific record in JavaScript?

AmitDiwan
Updated on 26-Oct-2020 11:15:22

360 Views

To fetch a specific record, use find() with some condition.ExampleFollowing is the code −var obj=[    {       studentId:101,       studentName:"John"    },    {       studentId:102,       studentName:"Bob"    },    {       studentId:103,       studentName:"David"    } ] const result = obj.find(    (o) =>    {       return o.studentId === 102    } ); console.log(result);To run the above program, you need to use the below command −node fileName.js.Here, my file name is demo315.js.OutputThis will produce the following output −PS C:\Users\Amit\javascript-code> node demo315.js { studentId: 102, studentName: 'Bob' }

Advertisements