Categories
JavaScript Answers

How to convert a double to an int in JavaScript without rounding?

Sometimes, we want to convert a double to an int in JavaScript without rounding.

In this article, we’ll look at how to convert a double to an int in JavaScript without rounding.

How to convert a double to an int in JavaScript without rounding?

To convert a double to an int in JavaScript without rounding, we can use the parseInt function.

For instance, we write

const num = 2.9;
console.log(parseInt(num, 10));

to call parseInt with num and radix 10 to return an integer part of num as the value.

As a result, we see 2 logged from the console log.

Conclusion

To convert a double to an int in JavaScript without rounding, we can use the parseInt function.

Categories
JavaScript Answers

How to set the content-type of request header when using Fetch API with JavaScript?

Sometimes, we want to set the content-type of request header when using Fetch API with JavaScript.

In this article, we’ll look at how to set the content-type of request header when using Fetch API with JavaScript.

How to set the content-type of request header when using Fetch API with JavaScript?

To set the content-type of request header when using Fetch API with JavaScript, we can put the header in a Header object.

For instance, we write

const options = {
  method,
  headers: new Headers({ "content-type": "application/json" }),
  mode: "no-cors",
  body: JSON.stringify(body),
};
await fetch(url, options);

to call fetch to make a request to the url with the request method.

We set headers to a Headers object with the content-type header set to 'application/json'.

body is the stringified request body JSON.

Conclusion

To set the content-type of request header when using Fetch API with JavaScript, we can put the header in a Header object.

Categories
JavaScript Answers

How to call a JavaScript function from console?

Sometimes, we want to call a JavaScript function from console.

In this article, we’ll look at how to call a JavaScript function from console.

How to call a JavaScript function from console?

To call a JavaScript function from console, we just type in the code to define the function and call it.

For instance, we type

const myFunction = () => {
  alert("doing something!");
};
myFunction();

into the console to define the myFunction function and call it.

Then we should see an alert box that shows ‘doing something!’.

Conclusion

To call a JavaScript function from console, we just type in the code to define the function and call it.

Categories
JavaScript Answers

How to use CSS selector for targeting only immediate children and not other identical descendants?

Sometimes, we want to use CSS selector for targeting only immediate children and not other identical descendants.

In this article, we’ll look at how to use CSS selector for targeting only immediate children and not other identical descendants.

How to use CSS selector for targeting only immediate children and not other identical descendants?

To use CSS selector for targeting only immediate children and not other identical descendants, we can use the > operator.

For instance, we write

ul > li

to select the li elements that are the immediate children of a ul element.

Conclusion

To use CSS selector for targeting only immediate children and not other identical descendants, we can use the > operator.

Categories
JavaScript Answers

How to get HTML elements by their attribute names with JavaScript?

Sometimes, we want to get HTML elements by their attribute names with JavaScript.

In this article, we’ll look at how to get HTML elements by their attribute names with JavaScript.

How to get HTML elements by their attribute names with JavaScript?

To get HTML elements by their attribute names with JavaScript, we can use the querySelectorAll method.

For instance, we write

const els = document.querySelectorAll("[property]");
const els2 = document.querySelectorAll('[property="value"]');

to call querySelectorAll with "[property]" to select all the elements with the property attribute and return a node list with the elements.

And we call querySelectorAll with "[property="value"]" to select all the elements with the property attribute set to value and return a node list with the elements.

Conclusion

To get HTML elements by their attribute names with JavaScript, we can use the querySelectorAll method.