Found 9318 Articles for Object Oriented Programming

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

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

910 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

262 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

499 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

JavaScript - Check if value is a percentage?

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

980 Views

Let’s say the following is our value −var value="97%";To check the value for percentage, use regular expression.ExampleFollowing is the code −var value="97%"; var result=/^\d+(\.\d+)?%$/.test(value); if (result==true) {    console.log("The percent is="+value);   } else {    console.log("This is not percentage");   } var value1="percent"; var result1=/^\d+(\.\d+)?%$/.test(value1); if (result1==true) {    console.log("The percent is="+value1);   } else {    console.log("This is not percentage");   }To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo214.js.OutputThe output is as follows on console −PS C:\Users\Amit\JavaScript-code> node demo214.js The percent is=97% This is not percentageRead More

Behavior of + operator in JavaScript to store large numbers?

AmitDiwan
Updated on 03-Oct-2020 12:38:27

2K+ Views

To store large numbers in JavaScript, use BigInt() rather than + operator. If you will use the + operator, then expect loss of precision.Let’s say the following is our large number and we are storing using BigInt() −console.log("Loss of precision with + operator..")ExampleFollowing is the code −var stringValue1="100"; console.log("The integer value="); console.log(+stringValue1); var stringValue2="2312123211345545367"; console.log("Loss of precision with + operator..") console.log(+stringValue2); const storeLongInteger=BigInt("2312123211345545367"); console.log("No loss of precision with BigInt()"); console.log(storeLongInteger);To run the above program, you need to use the following command −node fileName.js. Here, my file name is demo212.js.OutputThe output is as follows on console −PS C:\Users\Amit\JavaScript-code> node demo213.js The ... Read More

Convert integer array to string array in JavaScript?

AmitDiwan
Updated on 01-Oct-2020 13:38:04

452 Views

To convert integer array to string array, use the map(String) in JavaScript. Let’s say the following is our integer array −var integerValues = [101, 50, 70, 90, 110, 90, 94, 68];Convert integer array to string array −integerValues.map(String);ExampleFollowing is the code −var integerValues = [101, 50, 70, 90, 110, 90, 94, 68]; console.log("The integer array value="); console.log(integerValues); integerValues.map(String); console.log("The string array value="); console.log(integerValues)To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo212.js.OutputThe output is as follows in console −PS C:\Users\Amit\JavaScript-code> node demo212.js The integer array value= [    101, 50, 70, 90, ... Read More

Remove json element - JavaScript?

AmitDiwan
Updated on 03-Nov-2023 13:48:38

24K+ Views

Let's say the following is our JSON string −var details = [    {       customerName: "Chris",       customerAge: 32    },    {       customerName: "David",       customerAge: 26    },    {       customerName: "Bob",       customerAge: 29    },    {       customerName: "Carol",       customerAge: 25    } ]To remove JSON element, use the delete keyword in JavaScript.ExampleFollowing is the complete code to remove JSON element −var details = [    {       customerName: "Chris",       ... Read More

Advertisements