Categories
JavaScript Answers

How to obtain smallest value from array in JavaScript?

Sometimes, we want to obtain smallest value from array in JavaScript.

In this article, we’ll look at how to obtain smallest value from array in JavaScript.

How to obtain smallest value from array in JavaScript?

To obtain smallest value from array in JavaScript, we can use the Math.min method.

For instance, we write

const arr = [14, 58, 20, 77, 55, 34, 25, 8];
const min = Math.min(...arr);
console.log(min);

to call Math.min with the arr array entries spread as arguments of Math.min to get the smallest number in arr.

Conclusion

To obtain smallest value from array in JavaScript, we can use the Math.min method.

Categories
JavaScript Answers

How to close window automatically after printing dialog closes with JavaScript?

Sometimes, we want to close window automatically after printing dialog closes with JavaScript.

In this article, we’ll look at how to close window automatically after printing dialog closes with JavaScript.

How to close window automatically after printing dialog closes with JavaScript?

To close window automatically after printing dialog closes with JavaScript, we call window.close when we move focus back onto the main window after printing.

For instance, we write

setTimeout(() => {
  window.print();
}, 500);

window.onfocus = () => {
  setTimeout(() => {
    window.close();
  }, 500);
};

to call window.print to open the browser print dialog.

Then we set window.onfocus to a function that runs when the window gets focus.

In it, we call window.close to close the window after a 500 ms delay in the setTimeout callback.

Conclusion

To close window automatically after printing dialog closes with JavaScript, we call window.close when we move focus back onto the main window after printing.

Categories
JavaScript Answers

How to run a single test file from command line with Karma and JavaScript?

Sometimes, we want to run a single test file from command line with Karma and JavaScript.

In this article, we’ll look at how to run a single test file from command line with Karma and JavaScript.

How to run a single test file from command line with Karma and JavaScript?

To run a single test file from command line with Karma and JavaScript, we can run a few commands.

First, we run

karma start

to start the Karma test server.

Then we run

karma run -- --grep=testDescriptionFilter

to run the test with the testDescriptionFilter text in the string that we call describe with to run those tests.

Conclusion

To run a single test file from command line with Karma and JavaScript, we can run a few commands.

Categories
JavaScript Answers

How to read the body of a Fetch promise with JavaScript?

Sometimes, we want to read the body of a Fetch promise with JavaScript.

In this article, we’ll look at how to read the body of a Fetch promise with JavaScript.

How to read the body of a Fetch promise with JavaScript?

To read the body of a Fetch promise with JavaScript, we can use the json method.

For instance, we write

const f = async () => {
  const response = await fetch("https://api.ipify.org?format=json");
  const data = await response.json();
  console.log(data);
};

to define function f that makes a get request with fetch.

Then we call response.json to return a promise with the response body object.

Finally, we use await to get the response body object and assign it to data.

Conclusion

To read the body of a Fetch promise with JavaScript, we can use the json method.

Categories
JavaScript Answers

How to get text box value in JavaScript?

Sometimes, we want to get text box value in JavaScript.

In this article, we’ll look at how to get text box value in JavaScript.

How to get text box value in JavaScript?

To get text box value in JavaScript, we can use the value property of the text box.

For instance, we write

<input type="text" name="txtJob" id="txtJob" />

to add an input element.

Then we write

const jobValue = document.getElementsByName("txtJob")[0].value;

to select the input boxes with name attribute txtJob with getElementsByName.

Then we use index 0 to get the first element.

And we get the value of the input with value.

Conclusion

To get text box value in JavaScript, we can use the value property of the text box.