AmitDiwan has Published 10744 Articles

How to display JavaScript array items one at a time in reverse on button click?

AmitDiwan

AmitDiwan

Updated on 09-Sep-2020 13:16:02

785 Views

Let’s say the following is our array −var listOfNames = [    'John',    'David',    'Bob' ];Following is our button −Click The Button To get the Reverse ValueNow, for array items in reverse, at first, reach the array length and decrement the length by 1. Then, print the contents ... Read More

Assign multiple variables to the same value in JavaScript?

AmitDiwan

AmitDiwan

Updated on 09-Sep-2020 13:10:44

4K+ Views

To assign multiple variables to the same value, the syntax is as followsvar anyVariableName1, anyVariableName2, anyVariableName3……….N; yourVariableName1=yourVariableName2=yourVariableName3=.........N=yourValue;Let’s say the following are our variables and we are assigning same value −var first, second, third, fourth, fifth; first=second=third=fourth=fifth=100;Examplevar first, second, third, fourth, fifth; first=second=third=fourth=fifth=100; console.log(first); console.log(second); console.log(third); console.log(fourth); console.log(fifth); console.log("The sum of ... Read More

Creating a JavaScript Object from Single Array and Defining the Key Value?

AmitDiwan

AmitDiwan

Updated on 09-Sep-2020 13:09:40

151 Views

To convert a JavaScript object to key value, you need to use Object.entries() along with map(). Following is the code −Examplevar studentObject={    101: "John",    102: "David",    103: "Bob" } var studentDetails = Object.assign({}, studentObject) studentDetails = Object.entries(studentObject).map(([studentId, studentName])=>({studentId ,studentName})); console.log(studentDetails);To run the above program, you need to ... Read More

Formatting text to add new lines in JavaScript and form like a table?

AmitDiwan

AmitDiwan

Updated on 09-Sep-2020 13:08:23

374 Views

For this, use map() along with join(‘’).Then ‘’ is for new line. Following is the code −ExamplestudentDetails = [    [101, 'John', 'JavaScript'],    [102, 'Bob', 'MySQL'] ]; var studentFormat = '||Id||Name||subjName||'; var seperate = ''; seperate = seperate + studentDetails.map(obj => `|${obj.join('|')}|`).join(''); studentFormat = studentFormat + seperate; console.log(studentFormat);To run ... Read More

Create new array without impacting values from old array in JavaScript?

AmitDiwan

AmitDiwan

Updated on 09-Sep-2020 13:07:04

275 Views

Let’s say the following is our current array −var listOfNames=["John", "Mike", "Sam", "Carol"];Use JSON.parse(JSON.stringify()) to create new array and set values from the old array above.Examplefunction createNewArray(listOfNames) {    return JSON.parse(JSON.stringify(listOfNames)); } var listOfNames=["John", "Mike", "Sam", "Carol"]; var namesArray = listOfNames.slice(); console.log("The new Array="); console.log(namesArray);To run the above program, you ... Read More

Display whether the office is closed or open right now on the basis of current time with JavaScript ternary operator

AmitDiwan

AmitDiwan

Updated on 09-Sep-2020 13:04:42

355 Views

Let’s say, we are matching the current date and time with the business hours. We need to display whether the office is closed or open right now on the basis of current time.Get the hours from the current date and can use the ternary operator for close and open. Following ... Read More

Counting elements of an array using a recursive function in JS?

AmitDiwan

AmitDiwan

Updated on 09-Sep-2020 13:02:23

611 Views

The recursive function calls itself with some base condition. Let’s say the following is our array with marks −var listOfMarks=[56, 78, 90, 94, 91, 82, 77];Following is the code to get the count of array elements −Examplefunction countNumberOfElementsUsingRecursive(listOfMarks) {    if (listOfMarks.length == 0) {       return 0; ... Read More

Get Random value from a range of numbers in JavaScript?

AmitDiwan

AmitDiwan

Updated on 09-Sep-2020 13:00:42

234 Views

Let’s say, first, we will set the start and end range and call the function:console.log(getRandomValueBetweenTwoValues(400, 480))We have passed start value 400 and end value 480. Let’s get the random value with Math.random() in JavaScript −Examplefunction getRandomValueBetweenTwoValues(startRange, endRange) {    return Math.floor(Math.random() * (endRange - startRange + 1) + startRange); } ... Read More

Check if a string has white space in JavaScript?

AmitDiwan

AmitDiwan

Updated on 09-Sep-2020 12:59:13

4K+ Views

To check whitespace in a string, use the concept of indexOf(‘ ’). Following is the code −Examplefunction stringHasTheWhiteSpaceOrNot(value){    return value.indexOf(' ') >= 0; } var whiteSpace=stringHasTheWhiteSpaceOrNot("MyNameis John");    if(whiteSpace==true){       console.log("The string has whitespace");    } else {       console.log("The string does not have whitespace"); ... Read More

How to put variable in regular expression match with JavaScript?

AmitDiwan

AmitDiwan

Updated on 09-Sep-2020 12:57:59

1K+ Views

You can use match() with passing the variable name. The syntax is as follows −console.log(yourVariableName.match(yourMatchingVariableName));Examplevar senetence = 'My Name is John'; console.log("The actual value="); console.log(senetence); var matchWord = 'John'; console.log("The matching value="); console.log(matchWord); var matchRegularExpression = new RegExp(matchWord, 'g' ); console.log(senetence.match(matchWord));To run the above program, you need to use the ... Read More

Advertisements