Menu

JavaScript closest() method

JavaScript closest() method of the Element interface traverses the element until it finds a node that matches the specified CSS selector. 
Syntax of JavaScript closest method syntax is element.closest(css selector).

HTML code snippet


<article>
  <div id="article">
    This is article 
    <div id="title">
      Here is article title
      <div id="description">Here is article description</div>
    </div>
  </div>
</article>

JavaScript closest example

const desElement = document.getElementById("description");

// the closest ancestor with the id of "title"
console.log(desElement.closest("#title")); //this will return <div id="title">

// the closest ancestor which is a div in a div
console.log(desElement.closest("div div")); //this will return <div id="description">

// the closest ancestor which is a div and has a parent article
console.log(desElement.closest("article > div")); //this will return <div id="article">

// the closest ancestor which is not a div
console.log(desElement.closest(":not(div)")); //this will return <article>


Reference

-- https://developer.mozilla.org/en-US/docs/Web/API/Element/closest

Decimal number check using JavaScript

While working with numbers specially decimal number or price or balance. A developer may face an issue where number is not coming in decimal format, and just appearing in round figure or only with one digit after decimal. like 12.2, 20, and 5.5

In this situation how to handle this on frond-end and show balance or price always in two decimal digits. Below is the code sample. 


if (cardBalanceData) {
	console.log('card balance '+cardBalanceData.balance);
	var gcBalance = cardBalanceData.balance;
	gcBalance = (gcBalance.toString().indexOf('.') >= 0) ? gcBalance : gcBalance+'.00';
	console.log("card balance in 2 decimal value:: "+ gcBalance);
}

Here in above JavaScript code, line #4 checking the gcBalance has decimal (.) into this, in case gcBalance doesn't have decimal value then append .00 into gcBalance otherwise print gcBalance as it is. 

First Human on the Earth and Adam | ChatGPT

We asked ChatGPT "who was the first human on the earth?" And this is what we have receive as answer.

The concept of a "first human" is a complex one, as the evolution of humans from earlier hominids is a gradual and ongoing process that occurred over millions of years. 

The earliest known members of the Homo genus (of which modern humans are a part) are believed to have emerged in Africa around 2.5-3 million years ago. However, these early humans were very different from modern humans, both physically and in terms of their behavior and cognitive abilities.

The species Homo sapiens (modern humans) is believed to have emerged in Africa around 300,000 years ago, and gradually spread throughout the world over the following tens of thousands of years. However, it's important to note that even among early Homo sapiens, there was likely significant genetic diversity and variation in appearance, behavior, and culture.

Therefore, it's not possible to point to a single "first human" who emerged at a specific time and place. Rather, human evolution is an ongoing process that has produced a wide range of human forms and cultures over time.

who was the first human on the earth?
Who was the first human on the earth?



Then, we asked follow-up question "doesnt Adam was the first man?" and got this response;

The idea of Adam as the first man comes from religious traditions, specifically from the Abrahamic religions of Judaism, Christianity, and Islam. According to these traditions, Adam was created directly by God and was the first human being on Earth.

However, from a scientific perspective, the concept of Adam as the first man is not supported by the evidence. Human evolution is an ongoing process that occurred over millions of years, and there was no single individual who can be identified as the "first man" in a scientific sense. Instead, the evolution of humans involved the gradual development and diversification of populations over time. Therefore, while the concept of Adam as the first man is meaningful in religious contexts, it is not a scientifically supported idea.

doesnt Adam was the first man?
Doesnt Adam was the first man?