Found 9150 Articles for Object Oriented Programming

How to recognize when to use : or = in JavaScript?

AmitDiwan
Updated on 27-Oct-2020 10:03:29

262 Views

The colon (:) can be used when you want to define the property to an object while equal (=) can be used when you want to assign a value to a variable.ExampleFollowing is the code −var studentDetails = {    "studentId": 101,    "studentName": "John",    "studentSubjectName": "Javascript",    "studentCountryName": "US" } console.log(studentDetails); var firstName = "David"; console.log(firstName);To run the above program, you need to use the below command −node fileName.js.Here, my file name is demo325.js.OutputThis will produce the following output −PS C:\Users\Amit\javascript-code> node demo325.js {    studentId: 101,    studentName: 'John',    studentSubjectName: 'Javascript',    studentCountryName: 'US' } David

Remove all child elements of a DOM node in JavaScript?

AmitDiwan
Updated on 27-Oct-2020 09:58:58

260 Views

To remove child elements, set the innerHTML to ‘’.ExampleFollowing is the code −            Document Javascript MySQL Remove the items    remove.onclick = () => {       const element = document.getElementById("removeAllChildElements");       element.innerHTML = '';    } To run the above program, save the file name anyName.html(index.html). Right click on the file and select the option “Open with live server” in VS Code editor.OutputThis will produce the following output −Now you can click the button “Remove the items”. It will remove all the elements inside the box.OutputThis will produce the following output −

JavaScript map() is not saving the new elements?

AmitDiwan
Updated on 27-Oct-2020 09:55:09

133 Views

Use the map() function correctly to save elements.ExampleFollowing is the code −const addIndexValueToArrayElement = function (arrObject) {    const updatedArrayValue = [];    arrObject.forEach(function (ob) {       const mapValue = ob.map(function (value) {          return value + arrObject.indexOf(ob)       })       updatedArrayValue.push(mapValue)    })    return updatedArrayValue; }; const output = addIndexValueToArrayElement([    [4, 5],    [7, 56],    [34, 78], ]); console.log(output);To run the above program, you need to use the below command −node fileName.js.Here, my file name is demo323.js.OutputThis will produce the following output −PS C:\Users\Amit\javascript-code> node demo323.js [ [ 4, 5 ], [ 8, 57 ], [ 36, 80 ] ]

JavaScript input type=submit is not working?

Disha Verma
Updated on 25-Feb-2025 14:52:42

2K+ Views

The input type submit is not working; it is one of the common issues that every developer faces while working with JavaScript forms. To make it work, you can use an onclick event listener with input type = "submit" or prevent its default behaviour. This article looks at common reasons why a submit button might not work in JavaScript and offers solutions to resolve this issue. Common Reasons for Submit Button Issues The following are listed are some common reasons that may lead to this issue: Missing Event Listener − If you have not ... Read More

Repeat String in JavaScript?

AmitDiwan
Updated on 27-Oct-2020 09:40:39

149 Views

To repeat string, you can use Array() along with join().ExampleFollowing is the code −            Document    String.prototype.counter = function (value) {       return new Array(value + 1).join(this);    }    console.log("The repeat string".counter(5)); To run the above program, save the file name anyName.html(index.html). Right click on the file and select the option “Open with live server” in VS Code editor.OutputThis will produce the following output: on console −

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

75 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

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

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

2K+ 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

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

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

476 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' }

Array.prototype.fill() with object passes reference and not new instance in JavaScript?

AmitDiwan
Updated on 26-Oct-2020 11:08:01

173 Views

To fix this, you can use map() in JavaScript.The syntax is as follows −var anyVariableName= new Array(yourSize).fill().map(Object);ExampleFollowing is the code −var arrayOfObject = new Array(5).fill().map(Object); console.log(arrayOfObject);To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo311.js.OutputThis will produce the following output −PS C:\Users\Amit\javascript-code> node demo311.js [ {}, {}, {}, {}, {} ]

How to make a setInterval() stop after some time or after a number of actions in JavaScript?

AmitDiwan
Updated on 26-Oct-2020 11:06:41

2K+ Views

Use some conditions to stop after some time.The below code will stop in half a minute.ExampleFollowing is the code −            Document    var now = new Date().getTime();    var interval = setInterval(function () {       if (new Date().getTime() - now > 30000) {          clearInterval(interval);          return;       }       console.log("working");    }, 2000); To run the above program, save the file name anyName.html(index.html) and right click on the file. Select the option “Open with live server” in VS Code editor.OutputThis will produce the following output −

Advertisements