Categories
JavaScript Answers

How to convert a date to GMT with JavaScript?

Sometimes, we want to convert a date to GMT with JavaScript.

In this article, we’ll look at how to convert a date to GMT with JavaScript.

How to convert a date to GMT with JavaScript?

To convert a date to GMT with JavaScript, we use the toUTCString method.

For instance, we write

console.log(new Date("Fri Jan 20 2022 11:51:36 GMT-0500").toUTCString());

to call the toUTCString method to return a the date as a string in UTC.

Conclusion

To convert a date to GMT with JavaScript, we use the toUTCString method.

Categories
JavaScript Answers

How to mock constructor using Sinon and JavaScript?

Sometimes, we want to mock constructor using Sinon and JavaScript.

In this article, we’ll look at how to mock constructor using Sinon and JavaScript.

How to mock constructor using Sinon and JavaScript?

To mock constructor using Sinon and JavaScript, we call the sinon.stub method.

For instance, we write

const MockExample = sinon.stub();
MockExample.prototype.test = sinon.stub().returns("42");
const example = new MockExample();
console.log(example.test());

to create the MockExample constructor stub with stub.

Then we set its test instance method to return 42 with sinon.stub().returns("42");.

Then we create a MockExample instance and call its test method.

Conclusion

To mock constructor using Sinon and JavaScript, we call the sinon.stub method.

Categories
JavaScript Answers

How to listen to iframe onload JavaScript event?

Sometimes, we want to listen to iframe onload JavaScript event.

In this article, we’ll look at how to listen to iframe onload JavaScript event.

How to listen to iframe onload JavaScript event?

To listen to iframe onload JavaScript event, we set the iframe’s onload property to a function that’s called when the iframe is loaded.

For instance, we write

<iframe id="my_iframe" src="http://www.exanple.com/"> </iframe>

to add an iframe.

Then we write

document.getElementById("my_iframe").onload = () => {
  //...
};

to select the iframe with getElementById.

Then we set its onload property to a function that’s called when the iframe’s contents is loaded.

Conclusion

To listen to iframe onload JavaScript event, we set the iframe’s onload property to a function that’s called when the iframe is loaded.

Categories
JavaScript Answers

How to style active Link in react-router v4 and JavaScript?

Sometimes, we want to style active Link in react-router v4 and JavaScript.

In this article, we’ll look at how to style active Link in react-router v4 and JavaScript.

How to style active Link in react-router v4 and JavaScript?

To style active Link in react-router v4 and JavaScript, we use the NavLink component.

For instance, we write

<div id="tags-container">
  {tags.map((t) => (
    <NavLink className="tags" activeStyle={{ color: "red" }} to={t.path}>
      {t.title}
    </NavLink>
  ))}
</div>

to render NavLinks.

We set its activeStyle to the style that’s rendered when the current link is active.

Categories
JavaScript Answers

How to create line breaks in console.log() in Node.js and JavaScript?

Sometimes, we want to create line breaks in console.log() in Node.js and JavaScript.

In this article, we’ll look at how to create line breaks in console.log() in Node.js and JavaScript.

How to create line breaks in console.log() in Node.js and JavaScript?

To create line breaks in console.log() in Node.js and JavaScript, we log the '\n' character.

For instance, we write

console.log({ a: 1 }, "\n", { b: 3 }, "\n", { c: 3 });

to call console.log with '\n' separating each object to log each object in their own line.

Conclusion

To create line breaks in console.log() in Node.js and JavaScript, we log the '\n' character.