Categories
JavaScript Answers

How to test for object keys and values with Jest and JavaScript?

Sometimes, we want to test for object keys and values with Jest and JavaScript.

in this article, we’ll look at how to test for object keys and values with Jest and JavaScript.

How to test for object keys and values with Jest and JavaScript?

To test for object keys and values with Jest and JavaScript, we use the toMatchObject method.

For instance. we write

const expected = { name: "foo" };
const actual = { name: "foo", type: "form" };
expect(actual).toMatchObject(expected);

to call toMatchObject to check if any part of the actual object matches the expected object.

Conclusion

To test for object keys and values with Jest and JavaScript, we use the toMatchObject method.

Categories
JavaScript Answers

How to pass parameters to JavaScript files?

Sometimes, we want to pass parameters to JavaScript files.

In this article, we’ll look at how to pass parameters to JavaScript files.

How to pass parameters to JavaScript files?

To pass parameters to JavaScript files, we can store data in data attributes.

For instance, we write

<script id="helper" data-name="helper" src="helper.js"></script>

to add a script element with the data-name attribute.

Then we get the value of the data-name attribute with

const name = document.getElementById("helper").getAttribute("data-name");

We call getAttribute with 'data-name' to return the value of the data-name attribute after selecting the script element with getElementById.

Conclusion

To pass parameters to JavaScript files, we can store data in data attributes.

Categories
JavaScript Answers

How to strip all punctuation from a string in JavaScript using regex?

Sometimes, we want to strip all punctuation from a string in JavaScript using regex.

In this article, we’ll look at how to strip all punctuation from a string in JavaScript using regex.

How to strip all punctuation from a string in JavaScript using regex?

To strip all punctuation from a string in JavaScript using regex, we call replace with a regex to replace all the punctuation marks from a string.

For instance, we write

const newStr = str.replace(/[^\w\s]|_/g, "").replace(/\s+/g, " ");

to call replace with /[^\w\s]|_/g to match all instances of non word characters and spaces and replace them with empty strings.

And we call replace again to replace all whitespaces with empty strings.

replace returns the string with the replacement done.

Conclusion

To strip all punctuation from a string in JavaScript using regex, we call replace with a regex to replace all the punctuation marks from a string.

Categories
JavaScript Answers

How to wait for a promise to finish before returning the variable of a function with JavaScript?

Sometimes, we want to wait for a promise to finish before returning the variable of a function with JavaScript.

In this article, we’ll look at how to wait for a promise to finish before returning the variable of a function with JavaScript.

How to wait for a promise to finish before returning the variable of a function with JavaScript?

To wait for a promise to finish before returning the variable of a function with JavaScript, we can use await.

For instance, we write

const waitForPromise = async () => {
  let result = await Promise.resolve("hello");
};

to create the waitForPromise function that calls Promise.resolve to return a promise.

Then we use await to wait for the result of the promise and get the result before we continue running the function.

Conclusion

To wait for a promise to finish before returning the variable of a function with JavaScript, we can use await.

Categories
JavaScript Answers

How to suppress use of cookies for users who have not yet given consent with Google Analytics and JavaScript?

Sometimes, we want to suppress use of cookies for users who have not yet given consent with Google Analytics and JavaScript.

In this article, we’ll look at how to suppress use of cookies for users who have not yet given consent with Google Analytics and JavaScript.

How to suppress use of cookies for users who have not yet given consent with Google Analytics and JavaScript?

To suppress use of cookies for users who have not yet given consent with Google Analytics and JavaScript, we can switch on consent mode.

To do this, we write

gtag("consent", "default", {
  analytics_storage: "denied",
  ad_storage: "denied",
});

to call gtag with 'consent' and an object that denies cookies on users that haven’t consented to tracking with

{
  analytics_storage: "denied",
  ad_storage: "denied",
}

Conclusion

To suppress use of cookies for users who have not yet given consent with Google Analytics and JavaScript, we can switch on consent mode.