- 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
107 Views
Create a custom function to test a number is prime or not in JavaScript.ExampleFollowing is the code −function checkNumberIsPrime(number) { var flag = false; for (start = 2; start < number / 2; start++) { if (number % start === 0) { flag = false; break; } else { flag = true; } } return flag; } var number = 11; if (checkNumberIsPrime(number) == true) { console.log("The number is prime"); } else { console.log("The number is not prime"); }To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo230.js.OutputThe output is as follows −PS C:\Users\Amit\JavaScript-code> node demo230.js The number is prime
101 Views
The Math.ceil() is part of the Math object in JavaScript. Suppose, your value is 4.5 or 4.3, it will give the result 5.Let’s say the following are our values −var result1 = 98; var result2 = 5;Following is the code to display the result difference while using ceil() or not −var result1 = 98; var result2 = 5; console.log("The actual result is=" + result1 / result2); var output = Math.ceil(result1 / result2); console.log("The ceil result is=" + output);To run the above program, you need to use the following command −node fileName.js. Here, my file name is demo229.js.OutputThe output is as ... Read More
200 Views
Let’s say the following are our records with some duplicate values −var objectOfNationality = [ { nationality: "Indian" }, { nationality: "American" }, { nationality: "Emirati" }, { nationality: "Indian" }, { nationality: "American" } ];ExampleTo remove duplicate records, use the concept of Set().Following is the code −var objectOfNationality = [ { nationality: "Indian" }, { nationality: "American" }, { nationality: "Emirati" }, { nationality: "Indian" }, ... Read More
4K+ Views
Let’s say the following is our string in dot notation −const keys = "details1.details2.details3.details4.details5"And the following is our array −const firsName = "David";To turn into a nested object, use the concept of split(‘.’) along with map().ExampleFollowing is the code −const keys = "details1.details2.details3.details4.details5" const firsName = "David"; var tempObject = {}; var container = tempObject; keys.split('.').map((k, i, values) => { container = (container[k] = (i == values.length - 1 ? firsName : {})) }); console.log(JSON.stringify(tempObject, null, ' '));To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo227.js.OutputThe output is as ... Read More
177 Views
Let’s say we are getting two values with prompt −var firstvalue = parseInt(prompt("Enter the value1")); var secondvalue = parseInt(prompt("Enter the value2"));ExampleFollowing is the code − Live Demo Document var firstvalue = parseInt(prompt("Enter the value1")); var secondvalue = parseInt(prompt("Enter the value2")); if (firstvalue * secondvalue > 50) document.write("Result is correct"); else document.write("Result is not correct"); To run the above program, save the file name anyName.html (index.html). Right click on the file and select the option “Open ... Read More
895 Views
To find specific words in an array, you can use includes(). We have the following array −var sentence = ["My Name is John Smith. My Favourite Subject is JavaScript. I live in US. I like Hockey"];Now, the following is an array having the words we need to search in the above “sentence” array −var keywords = ["John", "AUS", "JavaScript", "Hockey"];ExampleFollowing is the code −var keywords = ["John", "AUS", "JavaScript", "Hockey"]; var sentence = ["My Name is John Smith. My Favourite Subject is JavaScript. I live in US. I like Hockey"]; const matched = []; for (var index = 0; index ... Read More
76 Views
You cannot change the value of an object when it is defined using const keyword.After changing it will remain same.Let’s say the following is our variable defined with const −const details1 = { firstName: 'David', subjectDetails: { subjectName: 'JavaScript' } }ExampleFollowing is the code to change the const variable, which would only display the initial value −const details1 = { firstName: 'David', subjectDetails: { subjectName: 'JavaScript' } } const details2 = { ...details1, subjectDetails: { ...details1.subjectDetails }, firstName: 'David' } details2.subjectDetails.subjectName = 'Java ' console.log(details1);To run the above program, you need to use the following command ... Read More
124 Views
Let’s say the following are our objects −var object1 = { firstName: "David" }; var object2 = { firstName: "David" };You will not get the correct result using comparison operator (== or ===). Use JSON.stringify() for this.ExampleFollowing is the code implementing both the ways and showing the correct result −var object1 = { firstName: "David" }; var object2 = { firstName: "David" }; if (object1 == object2) console.log("using == operator result ==> true"); else console.log("using == operator result ==> false"); if (JSON.stringify(object1) == JSON.stringify(object2)) console.log("using JSON.stringify() operator result ==> true"); else console.log("using JSON.stringify() operator result ==> ... Read More
2K+ Views
Let’s say the following are our array of objects:const details = [ { employeeFirstName: "John", employeeLastName: "Doe" }, { employeeFirstName: "David", employeeLastName: "Miller" }, { employeeFirstName: "John", employeeLastName: "Smith" } ]ExampleFollowing is the code to fetch specific values, in this case with first name “John” −const details = [ { ... Read More
63 Views
Let’s say the following is our array object −var arrayObject = [ "John", "David", "Mike" ]Use length property to set the length to 0 and clear memory.The syntax is as follows to clear memory and allocate again −yourArrayObjectName.length=0; // To clear memory yourArrayObjectName.length=4; // To allocate memoryExampleFollowing is the code −var arrayObject = [ "John", "David", "Mike" ] arrayObject.length = 0; console.log(arrayObject); arrayObject.length = 5; for (var i = 0; i < arrayObject.length; i++) console.log(arrayObject[i]);To run the ... Read More