No Result
View All Result
Cloud Reports
  • Home
  • Linux
  • Web development
  • Javascript
  • SQL
  • Ant Design tutorial
  • PC & Laptop
  • Technology & Digital
  • Home
  • Linux
  • Web development
  • Javascript
  • SQL
  • Ant Design tutorial
  • PC & Laptop
  • Technology & Digital
No Result
View All Result
Cloud Reports
No Result
View All Result

Javascript developer should stop for … in for now

npn by npn
December 31, 2020
in Javascript, Web development
Reading Time: 4min read
A A
0
Javascript developer should stop for … in for now

The content

READ ALSO

Understanding the difference between Number.isNaN and isNaN in javascript

Understanding the difference between Number.isNaN and isNaN in javascript

December 29, 2020
7
How to display colors (color, bgcolor) in console.log () for javascript developers!

How to display colors (color, bgcolor) in console.log () for javascript developers!

December 29, 2020
9
  • Ask readers
  • For in javascript
  • The first mistake is to use for in javascript
  • Mistake 2
  • The third mistake is with iterating enumerable properties. 

For in javascript. Loop is an inevitable tip when programming functions. But have you ever made the mistakes that we are about to say below, and how to fix those problems?

Requirements

  • Understand basic javascript
  • Ever used loop, probably for

For in javascript

Actually, for iterating in programming, we also said a lot about the positive things of using for in javascript. And we have also compared for in and for of in javascript. If you feel your knowledge is quite gaping, you can read the previous articles again, and then learn about mistakes when using for in this article.

JavaScript for … in to iterate enumerable properties ( enumerable properties mean? ) Of an object as follows:

### with object
var player = {
    two: "Messi",
    one: "Ronaldo",
    zero: "Name"
};

var countdown = "";

for (var step in player) {
    countdown += player[step] + "\n";
}

console.log(countdown);
// => "Messi
//    Ronaldo
//    Name!
//    "

Because for init works on any javascript object, so we can use it to loop one Arraylike the above example, we can do it again like this:

### with array

var player = [
    "Messi",
    "Ronaldo",
    "Name"
]

var countdown = "";

for (var step in player) {
    countdown += player[step] + "\n";
}

console.log(countdown);
// => "Messi
//    Ronaldo
//    Name!
//    "

Notes: Two different examples, take a closer look, object and array

The first mistake is to use for in javascript

Array.prototype.voice = "Rooney"; //enumerable properties

var player = [
    "Messi",
    "Ronaldo",
    "Name"
] // there are 3 item
s here

var countdown = "";

for (var step in player) {
    countdown += player[step] + "\n";
}

console.log(countdown);
// got 4 items.
//Messi
//Ronaldo
//Name
//Rooney

Many of you are still observant and know that you can solve this simply by using the hasOwnProperty.

ADVERTISEMENT
Array.prototype.voice = "Rooney"; //enumerable properties

var player = [
"Messi",
"Ronaldo",
"AnonyStick"
] // there are 3 item
s here

var countdown = "";

for (var step in player) {
if (player.hasOwnProperty(step)) {
countdown += player[step] + "\n";
}
}

console.log(countdown);
//got 3 items.
//Messi
//Ronaldo
//Name

But after you settle, can you go smoother and smoother? This question left for you? 

Mistake 2

Wrong order when the loop is completed. That is not really a problem for a normal object whose value is inherently out of order. But you probably don’t want your JavaScript engine to return array values ​​in random order because you might get an unexpected result like this:

If you have time to take a look at ecma-international.org I think you will understand. That is not really a problem for an ordinary object whose value is inherently out of order. 

But you probably don’t want your JavaScript engine to return your items in random order, because you might get unexpected results like this:

console.log(countdown);
// => "Name
//    Ronaldo
//    Messi
//    "

The third mistake is with iterating enumerable properties. 

As we discussed before, it is possible to store additional properties on an array. This can also lead to unexpected results.

For example:

var player = [
"Messi",
"Ronaldo",
"Name"
];

player.announcer = "Rooney";

var countdown = "";

for (var step in player) {
if (player.hasOwnProperty(step)) {
countdown += player[step] + "\n";
}
}

console.log(countdown);
// => "Messi
//Ronaldo
//Name
//Rooney

You can refer to adripofjavascript

ShareTweetShare
Previous Post

Understanding the difference between Number.isNaN and isNaN in javascript

Next Post

LG to present new transparent screens at CES 2021

npn

npn

Related Posts

Understanding the difference between Number.isNaN and isNaN in javascript
Javascript

Understanding the difference between Number.isNaN and isNaN in javascript

December 29, 2020
7
How to display colors (color, bgcolor) in console.log () for javascript developers!
Javascript

How to display colors (color, bgcolor) in console.log () for javascript developers!

December 29, 2020
9
What is Unit Test? Introduction and Example about Unit Test
Web development

What is Unit Test? Introduction and Example about Unit Test

December 24, 2020
6
If you want to learn React JS within 3 months, the following 8 basics must first be known
Javascript

If you want to learn React JS within 3 months, the following 8 basics must first be known

December 22, 2020
5
Accelerate your Continuous Integration pipelines with Directed Acyclic Graph (DAG)
Web development

Accelerate your Continuous Integration pipelines with Directed Acyclic Graph (DAG)

December 22, 2020
3
Modern JavaScript features should be used every day, for good resolution (P1).
Javascript

Modern JavaScript features should be used every day, for good resolution (P1).

December 21, 2020
8
Next Post

LG to present new transparent screens at CES 2021

No Result
View All Result

Categories

  • Android (1)
  • Ant Design tutorial (7)
  • Javascript (21)
  • Layout and Routing (2)
  • Linux (3)
  • PC & Laptop (34)
  • React (17)
  • SQL (2)
  • Technology & Digital (124)
  • The Basics (5)
  • Web development (38)

Search

No Result
View All Result
No Result
View All Result
  • Home
  • Linux
  • Web development
  • Javascript
  • SQL
  • Ant Design tutorial
  • PC & Laptop
  • Technology & Digital