Found 9054 Articles for Front End Technology

Is it correct to use JavaScript Array.sort() method for shuffling?

Nikitha N
Updated on 24-Jun-2020 06:40:54

42 Views

Yes, you can use the JavaScript Array.sort() method for shuffling. Let’s see howExamplefunction shuffleDisplay(arr) {    var tmp, current;        // calculating length    var top = arr.length;    if(top) while(--top) {       current = Math.floor(Math.random() * (top + 1));       tmp = arr[current];       arr[current] = arr[top];       arr[top] = tmp;    }    return arr; }

Why is [1,2] + [3,4] = “1,23,4” in JavaScript?

Arjun Thakur
Updated on 24-Jun-2020 06:41:33

47 Views

The JavaScript's + operator is used to add two numbers or join two strings. However, use the contact() method to join two arrays to get a new one. For example,[50, 70].concat([90, 100])The above prints,[50, 70, 90, 100]Let’s see your example. The + operator concats strings, and converts the arrays to strings −[1,2] + [3,4] '1,2' + '3,4' 1,23,4Or as mentioned above, use concat(),[1,2].concat([3,4]) [1,2,3,4]

How to display JavaScript variables in an HTML page without document.write?

Sravani S
Updated on 24-Jun-2020 06:42:02

212 Views

Use Element.innerHTML in JavaScript to display JavaScript variables in an HTML page without document.write.You can try to work through the following code snippet −var $myName = document.querySelector('.name'); var $jsValue = document.querySelector('.jsValue'); $myName.addEventListener('input', function(event){    $jsValue.innerHTML = $myName.value; }, false);

How to create SVG graphics using JavaScript?

Govinda Sai
Updated on 24-Jan-2020 06:52:31

393 Views

All modern browsers support SVG and you can easily create it using JavaScript. Google Chrome and Firefox both support SVG.With JavaScript, create a blank SVG document object model (DOM). Using attributes, create a shape like a circle or a rectangle.var mySvg = "http://www.w3.org/2000/svg"; var myDoc = evt.target.ownerDocument; var myShape = svgDocument.createElementNS(mySvg, "circle"); myShape.setAttributeNS(null, "cx", 40); myShape.setAttributeNS(null, "cy", 40); myShape.setAttributeNS(null, "r", 30); myShape.setAttributeNS(null, "fill", "yellow");

How to convert a binary NodeJS Buffer to JavaScript ArrayBuffer?

Chandu yadav
Updated on 24-Jun-2020 06:28:30

539 Views

Access the buf.buffer property directly to convert a binary NodeJS Buffer to JavaScript ArrayBuffer. The write through the original Buffer instance writes the ArrayBufferView.Keep in mind that the instances of Buffer are also instances of Uint8Array in node.js 4.x and higher versions.ExampleYou can try the following code snippet to convert a NodeJS buffer to JavaScript ArrayBuffer −function toArrayBuffer(myBuf) {    var myBuffer = new ArrayBuffer(myBuf.length);    var res = new Uint8Array(myBuffer);    for (var i = 0; i < myBuf.length; ++i) {       res[i] = myBuf[i];    }    return myBuffer; }

What is JavaScript garbage collection?

Vrundesha Joshi
Updated on 30-Jul-2019 22:30:21

175 Views

JavaScript automatically allocates memory, while a variable is declared. Garbage collection finds memory no longer used by the application and releases it since it is of no use. Garbage collector uses algorithms like Mark-and-sweep algorithm, to find the memory no longer used.This algorithm is used to free memory when an object is unreachable. The garbage collector identifies the objects, which are reachable or unreachable. These unreachable objects get the treatment from the automatic garbage collector.The Reference-Counting Garbage Collection is also used for garbage collection in JavaScript. The object will get automatically garbage collected if there are no references to it. ... Read More

How to workaround Objects vs arrays in JavaScript for key/value pairs?

Chandu yadav
Updated on 23-Jun-2020 13:21:48

113 Views

Store it like the following −var players = {    600 : 'Sachin',    300 : 'Brad', };For key/ value pairs, we have used the above solution, since we wanted a one-to-one. We did this to use the key as a lookup key. You can also add more values like this −var players = {    900 : 'Sachin',    300 : 'Brad',    700 : 'Steve',    200 : 'Rahul',    600 : 'Kevin',    500 : 'David', }

How to deal with Internet Explorer and addEventListener problem "Object doesn't support this property or method" in JavaScript?

George John
Updated on 23-Jun-2020 13:05:00

994 Views

To deal with “Object doesn’t support this property or method” issue in JavaScript, while using events and Internet Explorer, update your code with this −Example                      ...     You can also use attachEvent in IE to solve this issue like this −if (ev.addEventListener) {    ev.addEventListener('click', myText, false); } else if (ev.attachEvent) {     ev.attachEvent('onclick', myText); }

What is the easiest code for array intersection in JavaScript?

Srinivas Gorla
Updated on 23-Jun-2020 13:03:41

71 Views

For array intersection, you can try to run the following code in JavaScript −Example                    let intersection = function(x, y) {             x = new Set(x), y = new Set(y);             return [...x].filter(k => y.has(k));          };          document.write(intersection([5,7,4,8], [3,9,8,4,3]));          

What's the best way to detect a 'touch screen' device using JavaScript?

Chandu yadav
Updated on 23-Jun-2020 13:08:10

455 Views

The best way to detect a ‘touch screen’ device, is to work around to see whether the touch methods are present within the document model or not.function checkTouchDevice() {    return 'ontouchstart' in document.documentElement; }Here, you can also use it to detect mobile or desktop, like the following −if (checkTouchDevice()) {    // Mobile device } else {    // Desktop }

Advertisements