Found 8862 Articles for Front End Technology

How to get the length of an object in JavaScript?

vineeth.mariserla
Updated on 29-Jun-2020 15:08:09

2K+ Views

The length property is only applicable to arrays and strings. So when we call the length property on an object we will get undefined.ExampleLive Demo    var object = {prop:1, prop:2};    document.write(object.length); OutputundefinedWhereas arrays and strings will display their length when length property is used on them.ExampleLive Demo    var string = 'hello';    var array = [1, 2, 3];    var len1 = string.length;    var len2 = array.length;    document.write(len1);    document.write("");    document.write(len2); Output5 3In javascript, we have Object.keys() property, which checks whether there are any properties or not. If we use the length property ... Read More

How does JavaScript focus() method works?

vineeth.mariserla
Updated on 30-Jul-2019 22:30:26

1K+ Views

focus()Javascript focus() methods helps to highlight a HTML form element. It sets the element as an active element in the current document. In current documentation, focus can be applied to only one single element. The focus can be applied either to a text, a button, etc.  syntaxelement.focus(options);ExampleIn the following example, initially, the text "Tutorialspoint" is in an anchor tag 'a', which as an id called 'focus'. Later on, a function is declared in which the id is called using a DOM method and focus() method is applied on it to change the color of text "Tutorialspoint" to red. Here a button ... Read More

How to select a radio button by default in JavaScript?

Nikhilesh Aleti
Updated on 19-Sep-2022 07:57:16

12K+ Views

Radio button A radio button is used to select one among many options. This is one of the element in HTML. This radio button is a radio group to show a set of multiple choices, and in that only one can be selected. This way far similar to checkboxes, in checkboxes scenario’s we can select either one or many options as we can, whereas in radio button it can’t be done selecting multiple choices. We can define radio button is HTML using tag. Following is the syntax to define radio buttons in HTML Example 1 None selected ... Read More

Explain Generator functions in JavaScript?

vineeth.mariserla
Updated on 30-Jul-2019 22:30:26

526 Views

GeneratorsJavaScript supports Generator functions and Generator Objects. A generator function is the same as a normal function, but whenever it needs to generate a value it uses the 'yield' keyword rather than 'return'. The 'yield' keyword halts the function execution and sends a value back to the caller. It has an ability that it can resume the functionality from where it is left off. syntaxfunction* generator(){ yeild 1; yeild 2; }ExampleIn the following example, using a generator function, natural numbers 10, 9 and 8 were printed. Instead of printing each number individually we can run ... Read More

What is the importance of Math.trunc() method in JavaScript?

vineeth.mariserla
Updated on 30-Jul-2019 22:30:26

119 Views

Math.trunc()Unlike Math.floor(), Math.ceil() and Math.round(), Math.trunc() method removes fractional part and gives out only integer part. It doesn't care about rounding the number to the nearest integer. It doesn't even notice whether the value is positive or negative. Its function is only to chop up the fractional part.syntaxMath.trunc(x);parameters - Math.trunc() takes a number as an argument and truncates the fractional part.In the following example, Math.trunc() method truncated the fractional values of the provided positive numbers.ExampleLive Demo var val1 = Math.trunc("1.414"); var val2 = Math.trunc("0.49"); var val3 = Math.trunc("8.9"); document.write(val1); ... Read More

Is 'floating-point arithmetic' 100% accurate in JavaScript?

Nikhilesh Aleti
Updated on 19-Sep-2022 08:53:21

416 Views

Floating point A number which is either positive or negative whole number with having a decimal point to it is called floating point. Let’s consider these numbers, 1.52, 0.14 and also -98.345 are considered as floating point numbers. Whereas 77, 56, 90 and 0 are not considered as floating point numbers. In JavaScript this format will store the numbers in 64 bits. The numbers which are in fraction are stored in 0 to 51 bits. Coming to exponent, they are stored in 52 to 62 bits. And the sign bits will be stored in 63rd bit. Floating point arithmetic Floating ... Read More

How to use spread operator to join two or more arrays in JavaScript?

Nikhilesh Aleti
Updated on 16-Sep-2022 14:39:58

7K+ Views

Array is a variable. Which has an ability to store multiple values in it using appropriate syntax. Every value is referred with index numbers which will start from 0. Const ipl = [“Chennai”, “Mumbai”, Hyderabad”]; Spread operator (…) Spread operator can do the copying of one object or array’s properties and values to another object or array. This will be a shallow copy of the original. Const ipl = [“Chennai”, “Mumbai”, Hyderabad”]; Spread operator can do the copying of one object or array’s properties and values to another object or array. This will be a shallow copy of ... Read More

How to find maximum value in an array using spread operator in JavaScript?

Nikhilesh Aleti
Updated on 16-Sep-2022 13:22:49

935 Views

In this article, we are going to discuss how to find the maximum value in an array using the spread operator in JavaScript. For this you need to understand what is an array, what is spread operator and Math.max() method. Array An Array is a variable which is used to store different types of elements. Used to store a bunch of elements and access them with a single variable. Declaration of array can be done in below methods. var House = [ ]; // method 1 var House = new Array(); // method 2 Spread operator (…) This ... Read More

How to pass arrays as function arguments in JavaScript?

vineeth.mariserla
Updated on 30-Jul-2019 22:30:26

3K+ Views

Passing arrays as function argumentsIn olden days if we need to pass arrays as function arguments then apply() and null should be used. The use of null makes a code unclean. So to make code clean and also to pass an array as a function argument, the spread operator comes in to picture. By using the spread operator we don't need to use apply() function. Lets' discuss it in a nutshell.ExampleIn the following example, we have used null and apply() to pass an array as a function argument. This is an obsolete method. This method is replaced by a modern method in ... Read More

Can re-declaring a variable destroy the value of that variable in JavaScript?

vineeth.mariserla
Updated on 29-Jun-2020 12:47:46

236 Views

Re-declaring a variable will not destroy the value of a variable, until and unless it is assigned with some other new value.If we look at the following example variables "x" and ''y'' were assigned with values 4 and 8 respectively, later on when those variables were reassigned, the old values were replaced with the new values and displayed as shown in the output.ExampleLive Demo           var x = new Number(4);       var x = 7;       var y = 8;       var y = 10;       document.write(x);     ... Read More

Advertisements