Categories
JavaScript Answers

How to create a JavaScript variable reference or alias?

Sometimes, we want to create a JavaScript variable reference or alias.

In this article, we’ll look at how to create a JavaScript variable reference or alias.

How to create a JavaScript variable reference or alias?

To create a JavaScript variable reference or alias, we can create an object.

For instance, we write

const obj = { x: 1 };
const aliasToObj = obj;
aliasToObj.x++;
alert(obj.x);

to create the obj object and assign obj to aliasToObj.

aliasToObj references obj, so when we update its value of x with aliasToObj.x++.

obj.x also changes value.

Therefore, aliasToObj.x and obj.x are both 2.

Conclusion

To create a JavaScript variable reference or alias, we can create an object.

Categories
JavaScript Answers

How to get file name when user select a file from the file input with JavaScript?

Sometimes, we want to get file name when user select a file from the file input with JavaScript.

In this article, we’ll look at how to get file name when user select a file from the file input with JavaScript.

How to get file name when user select a file from the file input with JavaScript?

To get file name when user select a file from the file input with JavaScript.

For instance, we write

document.getElementById("fileInput").onchange = (e) => {
  console.log(e.target.value);
};

to select the file input with getElementById.

Then we set its onchange property to a function that get the selected file’s name with e.target.value.

Conclusion

To get file name when user select a file from the file input with JavaScript.

Categories
JavaScript Answers

How to detect shift+tab press with JavaScript?

Sometimes, we want to detect shift+tab press with JavaScript.

In this article, we’ll look at how to detect shift+tab press with JavaScript.

How to detect shift+tab press with JavaScript?

To detect shift+tab press with JavaScript, we can check if the shiftKey is true and the keyCode is 9.

For instance, we write

if (event.shiftKey && event.keyCode === 9) {
  // ...
}

to check if the shiftKey is pressed and while tab is pressed.

The keyCode for the tab key is 9.

event is the keyboard event object we get from the keyboard event handler callback’s parameter.

Conclusion

To detect shift+tab press with JavaScript, we can check if the shiftKey is true and the keyCode is 9.

Categories
JavaScript Answers

How to turn TypeScript object into JSON string?

Sometimes, we want to turn TypeScript object into JSON string.

In this article, we’ll look at how to turn TypeScript object into JSON string.

How to turn TypeScript object into JSON string?

To turn TypeScript object into JSON string, we can use the JSON.stringify method.

For instance, we write

const jsObj = { employee: { name: "John", age: 30, city: "New York" } };
const jsonString = JSON.stringify(jsObj);
console.log(jsonString);

to call JSON.stringify with the jsObj object to convert it to a JSON string.

Conclusion

To turn TypeScript object into JSON string, we can use the JSON.stringify method.

Categories
JavaScript Answers

How to change placeholder text with JavaScript?

Sometimes, we want to change placeholder text with JavaScript.

In this article, we’ll look at how to change placeholder text with JavaScript.

How to change placeholder text with JavaScript?

To change placeholder text with JavaScript, we can set the placeholder property.

For instance, we write

document.getElementsByName("Email")[0].placeholder = "new text for email";

to select the input with getElementsByName.

Then we get the first element with [0].

And then we set the placeholder property to the placeholder text we want to show.

Conclusion

To change placeholder text with JavaScript, we can set the placeholder property.