Categories
JavaScript Answers

How to create date from year, month, day with JavaScript?

Sometimes, we want to create date from year, month, day with JavaScript.

In this article, we’ll look at how to create date from year, month, day with JavaScript.

How to create date from year, month, day with JavaScript?

To create date from year, month, day with JavaScript, we can use the Date constructor.

For instance, we write

const d = new Date(2022, 11, 17);

to call the Date constructor with the year, month, and day.

The month starts with 0 for January and ends with 11 for December.

Conclusion

To create date from year, month, day with JavaScript, we can use the Date constructor.

Categories
React Answers

How to display binary data as image in React?

Sometimes, we want to display binary data as image in React.

In this article, we’ll look at how to display binary data as image in React.

How to display binary data as image in React?

To display binary data as image in React, we can set the src prop of the img element to a base64 URL.

For instance, we write

const data =
  "iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==";

const Example = ({ data }) => <img src={`data:image/jpeg;base64,${data}`} />;

to set the src prop of the img element in Example to a base64 URL with the binary image data.

Then the image will be displayed.

Conclusion

To display binary data as image in React, we can set the src prop of the img element to a base64 URL.

Categories
JavaScript Answers

How to pass a JavaScript variable value into input type hidden value?

Sometimes, we want to pass a JavaScript variable value into input type hidden value.

In this article, we’ll look at how to pass a JavaScript variable value into input type hidden value.

How to pass a JavaScript variable value into input type hidden value?

To pass a JavaScript variable value into input type hidden value, we can set its value property.

For instance, we write

<input type="hidden" id="myField" value="" />

to add a hidden input.

Then we write

document.getElementById("myField").value = product(2, 3);

to select the input with getElementById.

Then we set its value property to the return value of the product function to set the value attribute of the input.

Conclusion

To pass a JavaScript variable value into input type hidden value, we can set its value property.

Categories
JavaScript Answers

How to include a hyphen in a regex character bracket with JavaScript?

Sometimes, we want to include a hyphen in a regex character bracket with JavaScript.

In this article, we’ll look at how to include a hyphen in a regex character bracket with JavaScript.

How to include a hyphen in a regex character bracket with JavaScript?

To include a hyphen in a regex character bracket with JavaScript, we can use \-.

For instance, we write

/^[a-zA-Z0-9.\-_]+$/

to add hyphen into the list of characters to match in the square brackets.

Conclusion

To include a hyphen in a regex character bracket with JavaScript, we can use \-.

Categories
JavaScript Answers

How to use object types with Jasmine’s toHaveBeenCalledWith method with JavaScript?

Sometimes, we want to use object types with Jasmine’s toHaveBeenCalledWith method with JavaScript.

In this article, we’ll look at how to use object types with Jasmine’s toHaveBeenCalledWith method with JavaScript.

How to use object types with Jasmine’s toHaveBeenCalledWith method with JavaScript?

To use object types with Jasmine’s toHaveBeenCalledWith method with JavaScript, we can use spies.

For instance, we write

class Klass {
  method(arg) {
    return arg;
  }
}

describe("spy behavior", () => {
  it("should spy on an instance method of a Klass", () => {
    const obj = new Klass();
    spyOn(obj, "method");
    obj.method("foo argument");
    expect(obj.method).toHaveBeenCalledWith("foo argument");
    expect(
      obj.method.calls.mostRecent().args[0] instanceof String
    ).toBeTruthy();
  });
});

to create the Klass class to test.

Then we create a new Klass instance in the test.

We then spy on the method method with spyOn(obj, "method");.

Next, we call the method with obj.method("foo argument");.

Then we check that obj.method is called with 'foo argument' with expect(obj.method).toHaveBeenCalledWith("foo argument");.

And then we check that it’s most recently called with a string with

expect(obj.method.calls.mostRecent().args[0] instanceof String).toBeTruthy();

Conclusion

To use object types with Jasmine’s toHaveBeenCalledWith method with JavaScript, we can use spies.