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 = new Date(); console.log("The current date="+currentDate); var yesterdayDate = currentDate.setDate(currentDate.getDate()- 1); console.log("The yesterday date ="+new Date(yesterdayDate));To run the above program, you need to use the following command −node fileName.js.OutputHere, my file name is demo137.js. This will produce the following output −PS C:\Users\Amit\JavaScript-code> node demo137.js The current date=Fri Jul 31 2020 ... Read More
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++ ) { generatedOutput+=storedCharacters.charAt(Math.floor(Math.random() * totalCharacterSize)); } return generatedOutput; } console.log(generateRandom3Characters(3));To run the above program, you need to use the following command −node fileName.js.OutputHere, my file name is demo136.js. This will produce the following output −PS C:\Users\Amit\JavaScript-code> node demo136.js lq4
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"}, {isMarried: false}, {customerCollegeName: null} ]; const newCustomerDetails = customerDetails.slice(2, 4).concat(customerDetails[5]).map(obj=>({ propertyKey: Object.keys(obj)[0], propertyValue: Object.values(obj)[0] })); console.log(newCustomerDetails);To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo135.js.OutputThis will produce the following output −PS C:\Users\Amit\JavaScript-code> node demo135.js [ { propertyKey: ... Read More
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 var lastValue= $('table tr:last td').text(); console.log("The last value is="+lastValue); 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 −
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: 10px; } John Smith David Miller Adam Smith var studentNames = function() { var names = []; $(".demo").each(function() { names.push($(this).data('st')); }); return names.join(); }; console.log( studentNames() ); 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 −
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 after multiplying by 10 = "+ multiplyBy10); 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 −Put an integer value and click the OK button.After clicking the OK button, the snapshot is as follows −
To detect the screen resolution, use the concept of window.screen.For width, use the following −window.screen.availWidthFor height −window.screen.availHeightFollowing is the code to detect the screen resolution −Example Live Demo Document console.log("Available Width="+window.screen.availWidth); console.log("Available Height="+window.screen.availHeight); 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 −
The new keyword creates an object in JavaScript. To access values, use the dot notation. Following is the code −Examplefunction newObjectCreation() { this.firstName = "John"; } var newObject = new newObjectCreation(); newObject.firstName="David"; console.log("The first name="+newObject.firstName);To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo134.jsOutputThis will produce the following output −PS C:\Users\Amit\JavaScript-code> node demo134.js The first name=David
To attach event, use document.addEventListener() in JavaScript. Following is the code −Example Live Demo Document document.addEventListener('click',function(event){ if(event.target && event.target.id== 'buttonSubmit'){ console.log("Event generation on the button click") } }); 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 −After clicking the submit button, the screenshot is as follows −
Let’s say the following is our string −var sentence = "My, , , , , , , Name, , , , is John ,, , Smith";Use regular expression along with split() and join() to remove extra spaces and commas. Following is the code −Examplevar sentence = "My, , , , , , , Name, , , , is John ,, , Smith"; console.log("Before removing extra comma and space="+sentence); sentence = sentence.split(/[\s, ]+/).join(); console.log("After removing extra comma and space="); console.log(sentence)To run the above program, you need to use the following command −node fileName.js.OutputHere, my file name is demo133.js. This will produce ... Read More
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP