Found 6686 Articles for Javascript

Fetch alternative even values from a JavaScript array?

AmitDiwan
Updated on 03-Oct-2020 14:12:49

197 Views

To fetch alternative values, use the array index and check with the following condition −if(index%2==0) { // code } The above will display the even values.ExampleFollowing is the code −var subjectsName=["MySQL","JavaScript","MongoDB","C","C++","Java"]; for(let index=0;index node demo222.js Subject name at even position=MySQL Subject name at even position=MongoDB Subject name at even position=C++

Using the get in JavaScript to make getter function

AmitDiwan
Updated on 03-Oct-2020 14:11:36

125 Views

Use get keyword to make getter function.ExampleFollowing is the code −const studentDetails = {    studentName: "David Miller",    get studentName() {       console.log('I am calling the getter method...')    } } console.log(studentDetails.studentName);To run the above program, you need to use the following command −The output is as follows −node fileName.js.Here, my file name is demo221.js.OutputThe output is as follows −PS C:\Users\Amit\JavaScript-code> node demo221.js I am calling the getter method... Undefined

How to edit values of an object inside an array in a class - JavaScript?

AmitDiwan
Updated on 03-Oct-2020 14:08:19

208 Views

For this, use the “this” keyword.ExampleFollowing is the code −class Employee {     constructor() {         this.tempObject = [             {                 firstName: "David",                 setTheAnotherFirstName() {                     this.firstName = "Carol";                 },             },         ];     } } var empObject = new Employee(); empObject.tempObject[0].setTheAnotherFirstName(); console.log("The Change First Name is=" + empObject.tempObject[0].firstName);To run the above program, you need to use the following command −node fileName.js. Here, my file name is demo220.js.OutputThe output is as follows −PS C:\Users\Amit\JavaScript-code> node demo220.js The Change First Name is=Carol

Add property to common items in array and array of objects - JavaScript?

AmitDiwan
Updated on 03-Oct-2020 14:04:27

909 Views

To add property, use map(). Let’s say the following is our array −const firstname = ['John', 'David', 'Bob'];Following are our array of objects −const studentDetails = [    {       firstname: 'Carol',       marks: 78    },    {       firstname: 'Mike',       marks: 89    },    {       firstname: 'Bob',       marks: 86    } ]; ExampleFollowing is the code −const firstname = ['John', 'David', 'Bob']; const studentDetails = [    {       firstname: 'Carol',       marks: 78    },    {   ... Read More

How do I check that a number is float or integer - JavaScript?

AmitDiwan
Updated on 03-Oct-2020 14:00:24

915 Views

Let’s say we have the following variables −var value1 = 10; var value2 = 10.15;Use the Number() condition to check that a number is float or integer −Number(value) === value && value % 1 !== 0; }ExampleFollowing is the code −function checkNumberIfFloat(value) {    return Number(value) === value && value % 1 !== 0; } var value1 = 10; var value2 = 10.15; if (checkNumberIfFloat(value1) == true)    console.log("The value is float=" + value1); else    console.log("The value is not float=" + value1); if (checkNumberIfFloat(value2) == true)    console.log("The value is float=" + value2); else    console.log("The value is not ... Read More

Changing ternary operator into non-ternary - JavaScript?

AmitDiwan
Updated on 03-Oct-2020 13:56:18

264 Views

For ternary operator alternative, simply use if else in JavaScript. Let’s say, we have two numbers −var number1=12; var number2=12;To compare, we can use if else, instead of the ternary operator −if(number1==number2) console.log("true"); else console.log("false");ExampleFollowing is the code −var number1=12; var number2=12; var result=(number1==number2)?true:false; console.log(result); if(number1==number2)    console.log("true"); else    console.log("false");To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo217.js.OutputThe output is as follows −PS C:\Users\Amit\JavaScript-code> node demo217.js true trueRead More

How to new line string - JavaScript?

AmitDiwan
Updated on 03-Oct-2020 13:49:53

502 Views

Use tag for a new line.ExampleFollowing is the code −            Document           My Name is David Miller        document.getElementById("headingDemo").innerHTML = "My Favourite Subject is JavaScript" + '' + "I live in AUS."; 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.The output is as follows −

JavaScript: lexical scoping issue using new keyword constructor while adding inner function?

AmitDiwan
Updated on 03-Oct-2020 13:46:06

88 Views

To fix this, use the concept of this keyword. User another variable to hold the value of object, to use that inside the inner function.ExampleFollowing is the code −function Employee() {    this.technologyName = "JavaScript";    var currentTechnologyName = this;    function workingTechnology() {       console.log("I am working with " + currentTechnologyName.technologyName + " Technology");    }    workingTechnology(); } var currentTechnology = new Employee();To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo216.js.The output is as follows −PS C:\Users\Amit\JavaScript-code> node demo216.js I am working with JavaScript Technology

How do I console.log JavaScript variables as it relates to DOM?

AmitDiwan
Updated on 03-Oct-2020 13:41:56

90 Views

To display variables on console, use document.getElementById(“”).ExampleFollowing is the code − Live Demo            Document        const data = document.getElementById("printTheData");    console.log(data); 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 −Output in the console −

Looping numbers with object values and push output to an array - JavaScript?

AmitDiwan
Updated on 03-Oct-2020 13:10:05

96 Views

Let’s say the following are our numbers with object values −var numberObject = { 2:90 , 6: 98 }Use Array.from() in JavaScript −var fillThePositionValue = Array.from({length: 15}, (value, index) => numberObject[index+ 1] || "novalue")ExampleFollowing is the code to loop numbers with object values −var numberObject = { 2:90 , 6: 98 } console.log("The actual object is="); console.log(numberObject); var fillThePositionValue = Array.from({length: 15}, (value, index) => numberObject[index+ 1] || "novalue") console.log("After filling the value, the actual object is="); console.log(fillThePositionValue)To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo215.js.OutputThe output is as follows ... Read More

Advertisements