AmitDiwan has Published 10740 Articles

Update JavaScript object with another object, but only existing keys?

AmitDiwan

AmitDiwan

Updated on 11-Sep-2020 07:02:32

951 Views

For this, use hasOwnProperty(). Following is the code −Examplevar markDetails1 ={    'marks1': 78,    'marks2': 65 }; var markDetails2 ={    'marks2': 89,    'marks3': 90 } function updateJavaScriptObject(details1, details2) {    const outputObject = {};    Object.keys(details1)    .forEach(obj => outputObject[obj] =    (details2.hasOwnProperty(obj) ? details2[obj] : details1[obj])); ... Read More

How to split string when the value changes in JavaScript?

AmitDiwan

AmitDiwan

Updated on 11-Sep-2020 06:52:03

223 Views

For this, you can use match() along with regular expression. Following is the code −Examplevar originalString="JJJJOHHHHNNNSSSMMMIIITTTTHHH"; var regularExpression=/(.)\1*/g; console.log("The original string="+originalString); var splitting=originalString.match(regularExpression); console.log("After splitting the original string="); console.log(splitting);To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo139.js.OutputThis will produce the ... Read More

How to find the second largest element in a user-input JavaScript array?

AmitDiwan

AmitDiwan

Updated on 11-Sep-2020 06:50:14

570 Views

Let’s say following is our array −var numbers=[10, 50, 80, 60, 89];To find the second largest element, the code is as follows −Examplevar numbers=[10, 50, 80, 60, 89]; var firstLargerNumber = Number.MIN_SAFE_INTEGER; var secondlargerNumber = firstLargerNumber; for(var tempNumber of numbers){    if(tempNumber > firstLargerNumber){       secondlargerNumber = firstLargerNumber; ... Read More

Trigger a button click and generate alert on form submission in JavaScript

AmitDiwan

AmitDiwan

Updated on 11-Sep-2020 06:47:17

11K+ Views

Let’s say the following is our button −SubmitOn the basis of button id, on form submission, generate an alert −$("#submitForm").click(function() {    alert("The Form has been Submitted."); });Example Live Demo Document CollegeId= CollegeName=  Submit    $("#submitForm").click(function() {       alert("The ... Read More

How to get yesterday’s date in JavaScript?

AmitDiwan

AmitDiwan

Updated on 11-Sep-2020 06:45:22

406 Views

To get the yesterday date, first of all get the current date and subtract 1 from the current and use the function setDate(). The syntax is as follows −yourCurrentDateVariableName.setDate(yourCurrentDateVariableName.getDate() - 1);At first, get the current date −var currentDate = new Date();Now, get yesterday’s date with the following code −Examplevar currentDate ... Read More

Generate random characters and numbers in JavaScript?

AmitDiwan

AmitDiwan

Updated on 11-Sep-2020 06:43:15

442 Views

At first, set the characters and numbers −var storedCharacters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';To generate randomly, use Math.random(). Following is the code −Examplefunction generateRandom3Characters(size) {    var generatedOutput= '';    var storedCharacters =    '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';    var totalCharacterSize = storedCharacters.length;    for ( var index = 0; index < size; index++ ) { ... Read More

Assigning values to a computed property in JavaScript?

AmitDiwan

AmitDiwan

Updated on 11-Sep-2020 06:41:58

216 Views

Following is our object −const customerDetails=[    {customerFirstName: "David"},    {customerLastName: "Miller"},    {customerCountryName: "US"},    {customerAge: "29"},    {isMarried: false},    {customerCollegeName: null} ];Let’s assign values to a computed property using slice() along with map().Exampleconst customerDetails=[    {customerFirstName: "David"},    {customerLastName: "Miller"},    {customerCountryName: "US"},    {customerAge: "29"},   ... Read More

Get the last item from node list without using length property in JavaScript?

AmitDiwan

AmitDiwan

Updated on 11-Sep-2020 06:40:32

468 Views

Let’s say the following is our table − John David Mike Use the following with text() to get last item −$('table tr:last td')Example Live Demo Document John David Mike ... Read More

Assigning function to variable in JavaScript?

AmitDiwan

AmitDiwan

Updated on 11-Sep-2020 06:37:31

242 Views

Use return statement to assign function to variables. Following is the code −Example Live Demo Document    .demo {       background: skyblue;       height: 20px;       width: 75%;       margin: 10px;       padding: ... Read More

Fetch value from Alert pop up in jQuery

AmitDiwan

AmitDiwan

Updated on 11-Sep-2020 06:35:05

592 Views

To fetch value from alert pop up, use alert(). At first, use prompt() to input the value from user. Following is the code −Example Live Demo Document    var value = prompt("Please enter an integer value");    var multiplyBy10=10*value;    alert("Result ... Read More

Advertisements