Skip to main content

Javascript kiểm tra Array chính xác hơn bằng Array.isArray

Các dev Javascript có thể điều biết, arrays is not true arrays. Mọi thứ trong javascript đều là object. Một cách điều hiểu điều đó rõ nhất là sử dụng typeof để kiểm tra nó, và tất nhiên nó sẽ là object. LOL

Vậy làm sao để kiểm tra chính xác một phần tử có phải là array hay không trong javascript ?

Đó đã không còn là vấn đề với Array.isArray(). Đây là các đơn giản để kiểm tra chính xác giá trị có phải là mảng hay không.




Array is not true array

Hãy xem ví dụ dưới để hiểu hơn, array is not true array




Đó chính là lý do tại sao ta không lên dùng typeof để kiểm tra array.


Array.isArray() demo


Ví dụ dưới đây đều là array và đều return về true





Comments

Popular posts from this blog

State Department refugee database vulnerable to hacking, watchdog found

As the debate over President Trump’s order suspending refugee admissions intensifies, Fox News has learned of another potential vulnerability in the system: the State Department refugee database has been susceptible to hacking for years. The State Department internal watchdog, back in December 2016, sent a "classified management alert" regarding ongoing IT security vulnerabilities in the system. Two months later, the inspector general  released a related unclassified report, "Inspection of the Bureau of Population, Refugees, and Migration." "It's very unusual for action to be taken by the inspector general before the final report is reviewed by the bureau that's under investigation," former U.N. ambassador and Fox News contributor John Bolton ex...

Hacker takes over thousands of Printers; sends alerts to users

The hacker did it in a good faith and also informed HP about the vulnerability. A critical vulnerability in printers has led to the hacking of thousands of printing devices not at just one location or city but across the globe. Reportedly, the flaw was exploited by a hacker called Stackoverflowin, who managed to hack 150,000 printers at a global level. The hacking spree seems to be the hacker’s way to inform the world regarding the vulnerable nature of printers because as per his findings the Internet-connected printers are functioning without any firewall protection. This is the main reason why almost any hacker can exploit them, said the hacker. “People have done this in the past and sent racist flyers, etc. I’m not about that, I’m about helping people to fix their problem, but having a bit of fun at the same time ;) Everyone’s been cool about it and thanked me, to be honest,” claimed the hacker. To prove his point, Stackoverflowin used an automated script and sca...

Best Python Cheatsheet

Python là một trong những ngôn ngữ lập trình tốt nhất để học. Khi bạn bắt đầu, bảng tham chiếu một trang về các biến, phương pháp, và các tùy chọn định dạng có thể có khá tiện dụng.  Mong bạn thấy bài viết hữu ích ! Chúc bạn thành công !