Categories
JavaScript Answers

How to convert lowercase letter to upper case with JavaScript?

Sometimes, we want to convert lowercase letter to upper case with JavaScript.

In this article, we’ll look at how to convert lowercase letter to upper case with JavaScript.

How to convert lowercase letter to upper case with JavaScript?

To convert lowercase letter to upper case with JavaScript, we can use the string’s toUpperCase method.

For instance, we write:

const str = 'abc'
const newStr = str.toUpperCase();
console.log(newStr)

to call str.toUpperCase to return a string with characters in str converted to upper case.

Therefore, newStr is 'ABC'.

Conclusion

To convert lowercase letter to upper case with JavaScript, we can use the string’s toUpperCase method.

Categories
JavaScript Answers

How to add an HTML file input that only allows a user to select only one file?

Sometimes, we want to add an HTML file input that only allows a user to select only one file.

In this article, we’ll look at how to add an HTML file input that only allows a user to select only one file.

How to add an HTML file input that only allows a user to select only one file?

To add an HTML file input that only allows a user to select only one file, we can add a file input without the multiple attribute.

For instance, we write:

<input type="file" id="file" name="file" />

to add a file input that only allows us to select a single file.

Conclusion

To add an HTML file input that only allows a user to select only one file, we can add a file input without the multiple attribute.

Categories
JavaScript Answers

How to escape HTML with JavaScript?

Sometimes, we want to escape HTML with JavaScript.

In this article, we’ll look at how to escape HTML with JavaScript.

How to escape HTML with JavaScript?

To escape HTML with JavaScript, we can put the HTML string in an element.

Then we can get the escaped string from the innerHTML property.

For instance, we write:

const escapeHTML = (str) => {
  const p = document.createElement("p");
  p.appendChild(document.createTextNode(str));
  return p.innerHTML;
}
const escaped = escapeHTML('Test & Sample')
console.log(escaped)

to define the escapeHTML function that takes the str string as a parameter.

In it, we create a p element with createElement.

Then we call appendChild with the text node that we create from str with createTextNode.

Finally, we return the innerHTML property to return the escaped string.

Therefore, escaped is 'Test &amp; Sample'.

Conclusion

To escape HTML with JavaScript, we can put the HTML string in an element.

Then we can get the escaped string from the innerHTML property.

Categories
JavaScript Answers

How to format numbers with commas with JavaScript?

Sometimes, we want to format numbers with commas with JavaScript.

In this article, we’ll look at how to format numbers with commas with JavaScript.

How to format numbers with commas with JavaScript?

To format numbers with commas with JavaScript, we can use the toLocaleString method.

For instance, we write:

const no = 3456;
const noStr = no.toLocaleString();
console.log(noStr)

to call toLocaleString to return a number string with digit groups separated by commas.

Therefore, noStr is '3,456'.

Conclusion

To format numbers with commas with JavaScript, we can use the toLocaleString method.

Categories
JavaScript Answers

How to destructure an array of objects with JavaScript?

Sometimes, we want to destructure an array of objects with JavaScript.

In this article, we’ll look at how to destructure an array of objects with JavaScript.

How to destructure an array of objects with JavaScript?

To destructure an array of objects with JavaScript, we can use the array destructuring syntax.

For instance, we write:

const someArray = [{
  data: 1
}, {
  data: 2
}, {
  data: 3
}]
const [{
    data: array0
  },
  {
    data: array1
  },
  {
    data: array2
  }
] = someArray

console.log(array0, array1, array2)

to define the someArray array and get the data property values from each object with destructuring.

We assign the data property of the first someArray to array0.

And we assign the data property of the first someArray to array1.

Finally, we assign the data property of the first someArray to array2.

Therefore, console log logs 1 2 3.

Conclusion

To destructure an array of objects with JavaScript, we can use the array destructuring syntax.