Found 6685 Articles for Javascript

How to add an element to a JSON object using JavaScript?

AmitDiwan
Updated on 16-Feb-2023 15:29:18

26K+ Views

In this article, you will understand how to add an element to a JSON object using JavaScript. JSON object literals are keys and values are separated by a colon surrounded by curly braces {}. Example 1 In this example, we add an element to a json object using bracket notation, var jsonObject = { members: { host: "hostName", viewers: { userName1: "userData1", ... Read More

How to access object properties from result returned by async() function in JavaScript?

AmitDiwan
Updated on 16-Feb-2023 15:24:54

730 Views

In this article, you will understand how to access object properties from result returned by async() functions in JavaScript. An object property in JavaScript is a variable that is associated with the object itself, i.e. the properties have a name and value is one of the attributes linked with the property. Example 1 In this example, let’s understand how to access objects property using dot notation console.log("A function is created that returns promise object") const promiseFunction = (input) => { return new Promise((resolve, reject) => { return resolve({ ... Read More

How to calculate days left until next Christmas using JavaScript?

AmitDiwan
Updated on 16-Feb-2023 15:22:58

648 Views

In this article, you will understand how to calculate days left until next Christmas using JavaScript. The Date object works with dates and times. Date objects are created with new Date(). JavaScript will use the browser's time zone and display a date as a full text string. Example 1 In this example, we compute the time difference without functions. let todayDate = new Date(); console.log("Today's date is defined as: ", todayDate) let christmasYear = todayDate.getFullYear(); if (todayDate.getMonth() == 11 && todayDate.getDate() > 25) { christmasYear = christmasYear + 1; } let christmasDate = new Date(christmasYear, 11, ... Read More

How to add a property to a JavaScript object using a variable as the name?

AmitDiwan
Updated on 16-Feb-2023 15:18:48

202 Views

In this article, you will understand how to add a property to a JavaScript object using a variable as the name. Adding property to an object can be achieved by two methods. The first is the dot (.) notation and the second is using the brackets([]). Example 1 In this example, let’s use the dot (.) notation. var inputObject = {a: "value1"}; console.log("An object is created with properties: ", inputObject) inputObject.b = "value2"; console.log("After adding properties, the object now contains: ", inputObject) console.log(inputObject) Explanation Step 1 − Define an object namely inputObject. Step 2 − Add an ... Read More

How to add float numbers using JavaScript?

AmitDiwan
Updated on 16-Feb-2023 15:16:40

4K+ Views

In this article, you will understand how to add float numbers using JavaScript. Float values in JavaScript are defined as parseFloat(string). Example 1 In this example, let’s understand adding float values without using functions. let inputFloat1 = parseFloat(2.3) let inputFloat2 = parseFloat(3.5) console.log("The two float values are defined as ", inputFloat1 ,"and", inputFloat2) let result = inputFloat1 + inputFloat2 console.log("The sum of the float values is: ", result) Explanation Step 1 − Define two float values inputFloat1 and inputFloat2. Step 2 − Add the two float values using addition operator (+). Step 3 − Display the result. ... Read More

How to add a parameter to the URL in JavaScript?

AmitDiwan
Updated on 16-Feb-2023 15:14:49

24K+ Views

In this article, you will understand how to add a parameter to the URL in JavaScript. There are two methods to add parameters to an url: append() method and set() method. The append() method is used to specifically add key-value pairs to the url. The set() method adds the value to a specific key. If the key doesn't exist, a new key is created. If several keys exist, the value is updated for one of them and others are deleted. Example 1 Let's look at the append() method in this example let inputUrl = new URL('https://urlExample.com?key1=value1'); let inputParams = new ... Read More

How to access the first value of an object using JavaScript?

AmitDiwan
Updated on 16-Feb-2023 15:12:13

8K+ Views

In this article, you will understand how to access the first value of an object using JavaScript. The first value of the object is the first property located at position[0] of the object. The object can be a key-value object or an array object. Example 1 In this example, let's consider a key-value pair object. const inputObject = {1: 'JavaScript', 2: 'Python', 3: 'HTML'}; console.log("A key-value pair object is defined and its values are: ", inputObject) console.log("The first value of the object is: ") const firstValue = Object.values(inputObject)[0]; console.log(firstValue); Explanation Step 1 − Define a key-value pair ... Read More

How to access an object having spaces in the object’s key using JavaScript?

AmitDiwan
Updated on 16-Feb-2023 15:10:06

896 Views

In this article, you will understand how to access an object having spaces in the object’s key using JavaScript. In such cases, we use bracket notation ‘[]’ to access objects or we use dot notation(.) to access objects. Let’s see a few examples below. Example 1 In this example, let's use the bracket notation[] to access objects. console.log("The input object is a key value pair with key as firstName and value as Joseph"); const inputObject = {'firstName': 'Joseph'}; console.log("Using bracket notation to access value") console.log(inputObject['firstName']); console.log("Using bracket notation to change the value to Alice") inputObject['firstName'] = 'Alice'; ... Read More

How many numbers in the given array are less/equal to the given value using the percentile formula in Javascript?

AmitDiwan
Updated on 16-Feb-2023 15:04:53

117 Views

In this article, you will understand how many numbers in the given array are less/equal to the given value using the percentile formula. We calculate the percentage of numbers in the given array less or equal to the number using the formula − Percentile = (n/N) * 100 Where, n is the number of values below x and N is the total number of values. Example 1 In this example, we use a for-loop to iterate the array and check each element whether the value is less, equal or greater than the given input value. const calculationPercentile = ... Read More

How ES6 (ES2015) evolved and brought new features to modern day JavaScript?

AmitDiwan
Updated on 16-Feb-2023 14:59:50

71 Views

In this article, you will understand how ES6 (ES2015) evolved and brought new features to modern day JavaScript. ES6 stands for ECMAScript 6. It is the 6th version of ECMAScript and was created to standardize the JavaScript. The top 10 features of ES6 are: let and const keywords, Arrow Functions, Multi-line Strings, Default Parameters, Template Literals, Destructuring Assignment, Enhanced Object Literals, Promises. Example 1 In this example, let’s demonstrate the Arrow function(=>) − console.log("An Arrow function Square has been defined") square = (x) => { return x * x; } let inputValue = 6 console.log("The input ... Read More

Advertisements