Categories
JavaScript Answers

How to Check if an Object is not an Array in JavaScript?

Sometimes, we want to check if an object is not an array in JavaScript

In this article, we’ll look at how to check if an object is not an array in JavaScript.

Check if an Object is not an Array in JavaScript

To check if an object is not an array in JavaScript, we can use the Array.isArray method.

For instance, we write:

const arr = [1, 2, 3];
console.log(!Array.isArray(arr));

const obj = {}
console.log(!Array.isArray(obj));

We call Array.isArray with arr as its argument to see if arr is an array.

Then we negate the returned value to check if it’s not an array.

Since arr is an array, the console log should log false .

Then we call Array.isArray with obj as its argument to see if obj is an array.

Then we negate the returned value to check if it’s not an array.

Since obj is not an array, the console log should log true.

Conclusion

To check if an object is not an array in JavaScript, we can use the Array.isArray method.

Categories
JavaScript Answers

How to Detect if a JavaScript String Contains Any Spaces?

Sometimes, we want to detect if a JavaScript string contains any spaces.

In this article, we’ll look at how to detect if a JavaScript string contains any spaces.

Detect if a JavaScript String Contains Any Spaces

To detect if a JavaScript string contains any spaces, we can use the JavaScript regex test method.

For instance, we can write:

const str = 'foo bar'
console.log(/\s/.test(str))

We call .test on the /\s/ regex to check if str has any spaces.

Since str has a space, the console log should log true .

Conclusion

To detect if a JavaScript string contains any spaces, we can use the JavaScript regex test method.

Categories
JavaScript Answers

How to Check for Canadian Postal Code Strings with JavaScript?

Sometimes, we want to check for Canadian postal code strings with JavaScript.

In this article, we’ll look at how to check for Canadian postal code strings with JavaScript.

Check for Canadian Postal Code Strings with JavaScript

To check for Canadian postal code strings with JavaScript, we can create our own regex and use it.

For instance, we can write:

const regex = /^[A-Za-z]\d[A-Za-z][ -]?\d[A-Za-z]\d$/;

console.log(regex.test('45464'))
console.log(regex.test('A1A 1A1'))

to create the regex where we check for 2 groups of 3 characters where we have 1 digit in between 2 letters in the first group, and a letter is in between 2 betters in the 2nd group.

And each group of characters is separated by a space, dash, or nothing.

^ indicates the beginning of the string.

[A-Za-z] matches upper and lower case letters.

d matches digits.

[ -]? optionally matches a space or dash.

$ indicates the end of the string.

Therefore, the first console log logs false and the 2nd one logs true .

Conclusion

To check for Canadian postal code strings with JavaScript, we can create our own regex and use it.

Categories
JavaScript Answers

How to Disable All Mouse Clicks on a Page with JavaScript?

Sometimes, we want to disable all mouse clicks on a page with JavaScript.

In this article, we’ll look at how to disable all mouse clicks on a page with JavaScript.

Disable All Mouse Clicks on a Page with JavaScript

To disable all mouse clicks on a page with JavaScript, we can call stopPropagation , stopImmediatePropagation and preventDefault in the event handler for the click and contextmenu events to disable left and right-click events respectively.

For instance, we can write:

$(document).click((e) => {
  e.stopPropagation();
  e.preventDefault();
  e.stopImmediatePropagation();
  return false;
});

$(document).bind('contextmenu', (e) => {
  e.stopPropagation();
  e.preventDefault();
  e.stopImmediatePropagation();
  return false;
});

We call $(document).click to add a click event handler.

And we call $(document).bind to add an event handler for the contextmenu event.

stopPropagation stops the propagation of the event to the top of the DOM tree.

preventDefault prevents the default action of the events.

And stopImmediatePropagation prevents other listeners of the same event from being called.

Conclusion

To disable all mouse clicks on a page with JavaScript, we can call stopPropagation , stopImmediatePropagation and preventDefault in the event handler for the click and contextmenu events to disable left and right-click events respectively.

Categories
JavaScript Answers

How to Get Information on a Single YouTube Video via JSON in JavaScript?

Sometimes, we want to get information on a single YouTube video via JSON in JavaScript.

In this article, we’ll look at how to get information on a single YouTube video via JSON in JavaScript.

Get Information on a Single YouTube Video via JSON in JavaScript

To get information on a single YouTube video via JSON in JavaScript, we can use fetch to fetch the data from YouTube’s JSON API.

For instance, we can write:

const videoId = 'VA770wpLX-Q';

(async () => {
  const res = await fetch(`https://www.youtube.com/oembed?url=http://www.youtube.com/watch?v=${videoId}&format=json
`)
  const data = await res.json()
  console.log(data)
})()

to fetch the JSON data with fetch from YouTube’s JSON API.

We just call res.json to convert the JSON data to an object.

Conclusion

To get information on a single YouTube video via JSON in JavaScript, we can use fetch to fetch the data from YouTube’s JSON API.