Found 6710 Articles for Javascript

Split Space Delimited String and Trim Extra Commas and Spaces in JavaScript?

AmitDiwan
Updated on 11-Sep-2020 06:25:40

385 Views

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

How do I display the content of a JavaScript object in a string format?

AmitDiwan
Updated on 11-Sep-2020 06:21:30

283 Views

Use document.getElementById() and display using innerHTML. Following is the code −Example Live Demo Document    var jsonObject="name";    var value = jsonObject.split(" ");    var add = {};    value.forEach(function(v){       add[v] ? add[v]++ : add[v] = "John";    });    document.getElementById("objectId").innerHTML = JSON.stringify(add); 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 −

JavaScript RegExp return values between various square brackets? How to get value brackets?

Disha Verma
Updated on 07-Mar-2025 13:02:17

2K+ Views

Extracting values between various square brackets can be done using JavaScript regular expressions. The regular expressions in JavaScript are useful for extracting values enclosed within different types of brackets, such as square brackets ([ ]), curly brackets ({ }), and parentheses "( )". In this article, we'll look at how to use JavaScript's RegExp to find and get content that is inside square brackets easily. Understanding JavaScript RegExp Regular expressions (RegExp) are patterns used to match and manipulate strings in JavaScript. JavaScript RegExp helps you find and get text that is inside specific brackets by using patterns. The ... Read More

Updating copied object also updates the parent object in JavaScript?

AmitDiwan
Updated on 10-Sep-2020 07:30:11

267 Views

No, the parent object won’t get updated. Use Object.assign() with some parameters and check. Following is the code −Examplevar firstObject = { name: 'John' }; var secondObject = { name: 'Carol' }; console.log("Before merging="); console.log(firstObject); var afterMerging = Object.assign({}, firstObject, secondObject); afterMerging.name = 'Smith'; console.log("After merging="); console.log(firstObject);To run the above program, you need to use the following command −node fileName.js.OutputHere, my file name is demo131.js. This will produce the following output −PS C:\Users\Amit\JavaScript-code> node demo131.js Before merging= { name: 'John' } After merging= { name: 'John' }

JavaScript Check for case insensitive values?

AmitDiwan
Updated on 10-Sep-2020 07:27:55

271 Views

To check for case insensitive values, use regular expression in JavaScript. Following is the code −Examplevar allNames = ['john','John','JOHN']; var makeRegularExpression = new RegExp(allNames.join( "|" ), "i"); var hasValue = makeRegularExpression.test("JOHN"); console.log("Is Present="+hasValue);To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo130.js.OutputThis will produce the following output −PS C:\Users\Amit\JavaScript-code> node demo130.js Is Present=true

How to subtract date from today's date in JavaScript?

AmitDiwan
Updated on 10-Sep-2020 07:20:14

1K+ Views

You need to use currentDate with the help of new Date().getDate(). The syntax is as follows −var anyVariableName= yourCurrentDate - yourSubstractDateOfCurrentMonth;Examplevar currentDate=new Date().getDate(); var substractDate=new Date("2020-07-01").getDate(); const numberOfDaysInCurrentMonthOnly = currentDate-substractDate; console.log(numberOfDaysInCurrentMonthOnly);NoteToday’s date is - 28-07-2020To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo129.jsPS C:\Users\Amit\JavaScript-code> node demo129.js 27

JavaScript how to get an alert to appear when I click on a button in a class?

Shriansh Kumar
Updated on 04-Oct-2024 14:51:12

2K+ Views

An alert in JavaScript is a dialog box that is displayed to the user when some important information or warnings needs to be notified. It may contain a message along with OK button. When you click on the OK button, it will dismiss the dialog. Clicking a button triggers an event handler which invoke a function that instructs the browser to display a dialog with a message. In this article, we will explore a few ways to get an alert to appear when user click on a button. For example, click on the button given below to get an ... Read More

How to use the map function to see if a time is in a certain time frame with JavaScript?

AmitDiwan
Updated on 10-Sep-2020 07:16:15

218 Views

Let’s say, we have subject records with the time when we are planning to study them −const scheduleDetails = [    { subjectName: 'JavaScript', studyTime: '5 PM - 11 PM' },    { subjectName: 'MySQL', studyTime: '12 AM - 4PM' } ]Here is how we can use the map(). Following is the code −Exampleconst scheduleDetails = [    { subjectName: 'JavaScript', studyTime: '5 PM - 11 PM' },    { subjectName: 'MySQL', studyTime: '12 AM - 4PM' } ] function timeToReadSubjectName(scheduleDetails) {    var currentTime = new Date().getHours();    let result = '';    scheduleDetails.map(obj => {       ... Read More

How to format JSON string in JavaScript?

AmitDiwan
Updated on 10-Sep-2020 07:14:36

1K+ Views

To format HSON string in JavaScript, use JSON.stringify() with some parameters. Following is the code −Examplevar details = {    studentId: 101,    studentFirstName: 'David',    studentLastName: 'Miller',    studentAge:21,    subjectDetails: {       subjectId: 'JavaScript_101',       subjectName: 'Introduction to JavaScript',    } }; console.log("Not Pretty JSON Format=") console.log(JSON.stringify(details)); console.log("Pretty JSON Format=") console.log(JSON.stringify(details, null, 3));To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo127.js.OutputThis will produce the following output −PS C:\Users\Amit\JavaScript-code> node demo127.js Not Pretty JSON Format= {"studentId":101, "studentFirstName":"David", "studentLastName":"Miller", "studentAge":21, "subj ectDetails":{"subjectId":"JavaScript_101", "subjectName":"Introduction to JavaScript"}} Pretty ... Read More

JavaScript to check consecutive numbers in array?

AmitDiwan
Updated on 10-Sep-2020 07:13:26

2K+ Views

To check for consecutive numbers like 100, 101, 102, etc., use the concept of reduce(). TRUE would be returned for consecutive numbers, else false is the return value.Exampleconst sequceIsConsecutive = (obj) => Boolean(obj.reduce((output, lastest) => (output ? (Number(output.number) + 1=== Number(lastest.number) ? lastest : false) : false))); console.log("Is Consecutive="+sequceIsConsecutive ([{ number: '100' }, {number: '101'} ,{number: '102' }])); console.log("Is Consecutive="+sequceIsConsecutive([{ number: '100' }, {number: '102'} ,{number: '104' }]));To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo126.js.OutputThis will produce the following output −PS C:\Users\Amit\JavaScript-code> node demo126.js Is Consecutive=true Is Consecutive=falseRead More

Advertisements