- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Found 6686 Articles for Javascript
310 Views
Let’s say the following is our string”var stringValue="453.000.00.00.0000";To convert the above string with zeroes to number, use parseInt() along with replace() −var numberValue = parseInt(stringValue.replace(/\./gm, '')); ExampleFollowing is the complete code −var stringValue="453.000.00.00.0000"; var numberValue = parseInt(stringValue.replace(/\./gm, '')); console.log("Original value="+stringValue) console.log("After modification the value="+numberValue);To run the above program, use the following command −node fileName.js. Here, my file name is demo239.js.OutputThe output is as follows −PS C:\Users\Amit\javascript-code> node demo239.js Original value=453.000.00.00.0000 After modification the value=45300000000000
4K+ Views
Let’s say the following is our string −const keyName = 'username';To set a string as a key for an object, use the [] and pass the string name −const stringToObject = { [keyName]: 'David Miller' };ExampleFollowing is the complete code −const keyName = 'username'; const stringToObject = { [keyName]: 'David Miller' }; console.log("Your String Value="+keyName); console.log("Your Object Value=") console.log(stringToObject);To run the above program, use the following command −node fileName.js.Here, my file name is demo238.js.OutputThe output is as follows −PS C:\Users\Amit\javascript-code> node demo238.js Your String Value=username Your Object Value= { username: 'David Miller' }Read More
605 Views
The best way to reduce and merge a collection of objects, use the concept of Object.values() along with reduce().Following is the object −var details = [ { studentId: 10, marks: 75, studentName: "John" }, { studentId: 10, marks: 75, studentName: "John" }, { studentId: 11, marks: 98, studentName: "Bob" } ];ExampleFollowing is the code to reduce and merge −var details = [ { studentId: 10, marks: 75, studentName: "John" }, { studentId: 10, marks: 75, studentName: "John" }, { studentId: 11, marks: 98, studentName: "Bob" } ]; output = Object.values(details.reduce((value, object) => { ... Read More
414 Views
Let’s say the following are our time data −var startHour = dayjs().hour(10) var endHour = dayjs().hour(22)To get the difference, use the diff() method −ExampleFollowing is the code − Live Demo Document var startHour = dayjs().hour(10) var endHour = dayjs().hour(22) console.log("The hours difference is=" + endHour.diff(startHour, "hours")); 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 −OutputThe output is as follows −
429 Views
Let’s say the following are ourrinput type checkbox −John David We want to check any of the checkbox. Use the checked property to check the checkbox.ExampleFollowing is the code − Live Demo Document John David document.querySelector('#checkedValue2').checked = true 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 −OutputThe output is as follows −
814 Views
Let’s say the following are our values −'6778922' '76633 56 1443' '8888 4532 3232 9999'We want the preceding characters to be replaced with 4 asterisks and the display rest of the last 3 characters. The output should be −**** 922 **** 443 **** 999For such conditions, use the replace() and set regex in it.ExampleFollowing is the code −const hideDataWithDot = value => value.replace(/.+(.{3})$/, "**** $1"); console.log(hideDataWithDot('6778922')) console.log(hideDataWithDot('76633 56 1443')) console.log(hideDataWithDot('8888 4532 3232 9999')) To run the above program, use the following command −node fileName.js. Here, my file name is demo236.js.OutputThe output is as follows −PS C:\Users\Amit\javascript-code> node demo236.js **** ... Read More
7K+ Views
Let’s say the following is our string with comma and whitespace −var sentences = " John , David , Bob , Mike, Carol ";To split the sentences by comma, use split(). For removing surrounding spaces, use trim().ExampleFollowing is the code −var sentences = " John , David , Bob , Mike, Carol "; console.log("The value=" + sentences); var result = sentences.split(", ").map(function (value) { return value.trim(); }); console.log("After modifying the value=") console.log(result);To run the above program, use the following command −node fileName.js.Here, my file ... Read More
136 Views
Let’s say the following is our array with duplicate elements −var duplicateNumbers = [10, 20, 100, 40, 20, 10, 100, 1000];We want the output to be −[10, 20, 100, 40, 1000];To display only the unique elements, use the concept of filter.ExampleFollowing is the code −var duplicateNumbers = [10, 20, 100, 40, 20, 10, 100, 1000]; console.log("With Duplicates Values="); console.log(duplicateNumbers); var noDuplicateNumbersArray = duplicateNumbers.filter(function (value, index, array) { return array.indexOf(value) === index; } ); console.log("Without Duplicates Values=") console.log(noDuplicateNumbersArray);To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo234.js.OutputThe output is as ... Read More
291 Views
This is the concept of Bubble Sort. It compares to adjacent element if it is lesser it will swap the value.ExampleFollowing is the code −var numbers = [10, 100, 30, 40, 90, 4, 91, 56, 78]; function bubbleSorting(numbers) { for (var outer = 0; outer < numbers.length; outer++) { for (var inner = 0; inner < numbers.length; inner++) { if (numbers[outer] < numbers[inner]) { var temp = numbers[outer]; numbers[outer] = numbers[inner]; numbers[inner] = temp; ... Read More
475 Views
For this, you can use for loop along with if condition. Let’s say the following is our array −var numbers = [1, 2, 34, 56, 78, 90, 100, 110, 40, 70, 67, 77, 34, 68, 89, 91, 94];We have set a counter to set a value. This value is set to display a fixed number of values −var counter = 6;The above shows the result will be 6 values.ExampleFollowing is the code −var numbers = [1, 2, 34, 56, 78, 90, 100, 110, 40, 70, 67, 77, 34, 68, 89, 91, 94]; var counter = 6; var newNumbers = []; var start = 0; for (var index = 0; index < numbers.length; index++) { if (index % 2 != 0) { start++; if (start node demo231.js [ 2, 56, 90, 110, 70, 77 ]