Categories
JavaScript Answers

How to initialize and using session storage in React and JavaScript?

Sometimes, we want to initialize and using session storage in React and JavaScript.

In this article, we’ll look at how to initialize and using session storage in React and JavaScript.

How to initialize and using session storage in React and JavaScript?

To initialize and using session storage in React and JavaScript, we can call the sessionStorage methods.

For instance, we write:

import React from "react";

export default function App() {
  React.useEffect(() => {
    sessionStorage.setItem("key", "value");
    console.log(sessionStorage.getItem("key"));
  }, []);

  return <></>;
}

to call sessionStorage.setItem with the key and value of the entry we want to store in session storage respectively.

Then we call sesessionStorage.getItem with the key to get the entry with the given key.

Conclusion

To initialize and using session storage in React and JavaScript, we can call the sessionStorage methods.

Categories
JavaScript Answers Vue Vue Answers

How to check if variable is empty or null with Vue v-if and JavaScript?

Sometimes, we want to check if variable is empty or null with Vue v-if and JavaScript.

In this article, we’ll look at how to check if variable is empty or null with Vue v-if and JavaScript.

How to check if variable is empty or null with Vue v-if and JavaScript?

To check if variable is empty or null with Vue v-if and JavaScript. we can set v-if to the variable we want to check.

For instance, we write:

<script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>

<div id='app'>

</div>

to add the Vue script and the app container.

Then we write:

const v = new Vue({
  el: '#app',
  template: `
    <div>
      <div v-if='archiveNote'>
      	{{archiveNote}}
      </div>
    </div>
  `,
  data: {
    archiveNote: undefined
  },
  mounted() {
    setTimeout(() => {
      this.archiveNote = 'note'
    }, 1000)
  }
})

to check if archiveNote is truthy before we show the div.

In the mounted hook, we set this.archiveNote to 'note' which will make it truthy in the setTimeout callback which will run in 1 second after mounted is run.

Conclusion

To check if variable is empty or null with Vue v-if and JavaScript. we can set v-if to the variable we want to check.

Categories
JavaScript Answers

How to get all global JavaScript variables?

Sometimes, we want to get all JavaScript global variables.

In this article, we’ll look at how to get all JavaScript global variables.

How to get all JavaScript global variables with JavaScript?

To get all JavaScript global variables, we can loop through the window object’s properties.

For instance, we write:

for (const [key, val] of Object.entries(window)) {
  console.log(key, val)
}

to call Object.entries with window to return an array of key-value pair in window as arrays.

In the loop, we log the key and val we destructured from the array.

Conclusion

To get all JavaScript global variables, we can loop through the window object’s properties.

Categories
JavaScript Answers

How to capture a CTRL-S press with JavaScript?

Sometimes, we want to capture a CTRL-S press with JavaScript.

In this article, we’ll look at how to capture a CTRL-S press with JavaScript.

How to capture a CTRL-S press with JavaScript?

To capture a CTRL-S press with JavaScript, we can listen to the keydown event.

For instance, we write:

document.onkeydown = (e) => {
  if (e.ctrlKey && e.key === 's') {
    e.preventDefault();
    console.log('CTRL + S');
  }
}

to set the document.onkeydown property to a function that checks if ctrl+s is pressed with e.ctrlKey && e.key === 's'.

If it’s true, we call e.preventDefault to stop the save file dialog from opening.

And then we log 'CTRL + S'.

Conclusion

To capture a CTRL-S press with JavaScript, we can listen to the keydown event.

Categories
JavaScript Answers

How to remove double quotes from default string with JavaScript?

Sometimes, we want to remove double quotes from default string with JavaScript.

In this article, we’ll look at how to remove double quotes from default string with JavaScript.

How to remove double quotes from default string with JavaScript?

To remove double quotes from default string with JavaScript, we can use the string replace method.

For instance, we write:

const test = "\"House\"";
const s = test.replace(/\"/g, "");
console.log(s)

to call test.replace with /\"/g and an empty string to replace all double quotes with empty strings.

Therefore, s is 'House'.

Conclusion

To remove double quotes from default string with JavaScript, we can use the string replace method.