Found 9312 Articles for Object Oriented Programming

What happens when length of object is set to 0 - JavaScript?

AmitDiwan
Updated on 09-Nov-2020 08:07:12

257 Views

Let’s say the following is our array object −var arrayObject =    [       "John",       "David",       "Mike"    ]You can use length property to set the length to 0 and clear memoryThe syntax is as follows to clear memory −yourArrayObjectName.length=0; // To clear memory yourArrayObjectName.length=4; // To allocate memoryOutputThis will produce the following output on console −var arrayObject =    [       "John",       "David",       "Mike"    ] arrayObject.length = 0; console.log(arrayObject); arrayObject.length = 5; for (var i = 0; i < arrayObject.length; i++)   ... Read More

How to make a condition for event 'click' inside/outside for multiple divs - JavaScript?

AmitDiwan
Updated on 09-Nov-2020 08:02:07

860 Views

You can use event listeners for clicks.ExampleFollowing is the code − Live Demo            Document           First Division               Second Division      document.addEventListener('click', callEventFuncion)    function callEventFuncion(event) {       var div = document.querySelectorAll('.divDemo');       var titleResult = document.querySelectorAll('.my-title');       var result = Array.apply(0, div).find((v) => v.contains(event.target));       if (result) {          console.log(" Incrementing Division Selection");       }       else { ... Read More

How to concatenate the string value length -1 in JavaScript.

AmitDiwan
Updated on 09-Nov-2020 07:54:56

233 Views

For this, use join(). It will concatenate the string value length-1.ExampleFollowing is the code −var count = 5; var values = new Array(count + 1).join('John'); console.log(values); var count1 = 5; var values1 = new Array(count1).join('John'); console.log(values1);To run the above program, you need to use the following command −node fileName.js. Here, my file name is demo274.js.OutputThis will produce the following output on console −PS C:\Users\Amit\javascript-code> node demo274.js JohnJohnJohnJohnJohn JohnJohnJohnJohn

Using JSON.stringify() to display spread operator result?

AmitDiwan
Updated on 09-Nov-2020 07:46:10

912 Views

With Spread Operator, allow the expression to expand to multiple arguments, elements, variables, etc.You can use JSON.stringify() to convert the JavaScript object to string. Here, we have our object as the result of using spread operator on details1 and details2.ExampleFollowing is the code −var details1 = { name: 'John', age: 21 }; var details2 = { countryName: 'US', subjectName:'JavaScript' }; var result= { ...details1, ...details2}; console.log(JSON.stringify(result));To run the above program, you need to use the following command −node fileName.js. Here, my file name is demo267.js.OutputThis will produce the following output on console −PS C:\Users\Amit\javascript-code> node demo267.js {"name":"John", "age":21, "countryName":"US", "subjectName":"JavaScript"}Read More

Validate Tutorialspoint URL via JavaScript regex?

AmitDiwan
Updated on 09-Nov-2020 07:31:44

155 Views

To validate a specific URL, use regular expression.ExampleThe code is as follows −function validateSoundURL(myURL) {    var regularExpression = /^https?:\/\/(tutorialspoint\.com)\/(.*)$/;    return myURL.match(regularExpression) && myURL.match(regularExpression)[2] } console.log(validateSoundURL("https://tutorialspoint.com/index")); console.log(validateSoundURL("https://tutorialspoint.com/java"));To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo259.js.OutputThis will produce the following output on console −PS C:\Users\Amit\javascript-code> node demo259.js index java

Is the !! (not not) operator in JavaScript equivalent to reverse process of not operator?

AmitDiwan
Updated on 09-Nov-2020 06:25:30

163 Views

Yes, the not not operator is the reverse process of not operator. If any value is true then single ! (not) will return false and !! will return opposite value (true).The not operator −var flag=true; console.log(!flag);The not not operator −var flag=true; console.log(!!flag);ExampleFollowing is the code −var flag=true; console.log("The result of single !=") console.log(!flag); console.log("The result of single !!=") console.log(!!flag)To run the above program, you need to use the following command −node fileName.js. Here, my file name is demo247.jsOutputThis will produce the following output on console −PS C:\Users\Amit\javascript-code> node demo247.js The result of single != false The result of single !!= ... Read More

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

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

209 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

214 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

97 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?

AmitDiwan
Updated on 27-Oct-2020 09:53:50

1K+ Views

To make it work, you can use onclick with input type = “submit”.ExampleFollowing is the code −            Document           First Name:             Submit        var firstName = null;    function submitForm(evnt) {       evnt.preventDefault();       firstName = document.getElementById("fName").value;       document.getElementById("result").innerHTML = "The First Name is=" + firstName;    } To run the above program, save the file name anyName.html(index.html). Right click on the file and select ... Read More

Advertisements