Categories
JavaScript Answers

How to call a JavaScript function named in a variable?

Sometimes, we want to call a JavaScript function named in a variable.

In this article, we’ll look at how to call a JavaScript function named in a variable.

How to call a JavaScript function named in a variable?

To call a JavaScript function named in a variable, we can get the function by the string key.

For instance, we write

function foo() {
  alert("foo");
}

const a = "foo";
window[a]();

to get the foo global function with window[a], which evaluates to window.foo.

And then we call that.

Conclusion

To call a JavaScript function named in a variable, we can get the function by the string key.

Categories
JavaScript Answers

How to check if anonymous object has a method with JavaScript?

Sometimes, we want to check if anonymous object has a method with JavaScript.

In this article, we’ll look at how to check if anonymous object has a method with JavaScript.

How to check if anonymous object has a method with JavaScript?

To check if anonymous object has a method with JavaScript, we can check with the typeof operator.

For instance, we write

const myObj = {
  prop1: "no",
  prop2() {
    return false;
  },
};

if (typeof myObj.prop2 === "function") {
  console.log("It's a function");
} else if (typeof myObj.prop2 === "undefined") {
  console.log("It's undefined");
} else {
  console.log("It's neither undefined nor a function");
}

to define the myObj object.

Then we check if myObj.prop2 is a function with the typeof operator.

To check if it’s a function, we write

typeof myObj.prop2 === "function"

to check if prop2 is a function.

Conclusion

To check if anonymous object has a method with JavaScript, we can check with the typeof operator.

Categories
JavaScript Answers

How to change the selected option of an HTML select element with JavaScript?

Sometimes, we want to change the selected option of an HTML select element with JavaScript.

In this article, we’ll look at how to change the selected option of an HTML select element with JavaScript.

How to change the selected option of an HTML select element with JavaScript?

To change the selected option of an HTML select element with JavaScript, we can set the value property of the select element.

For instance, we write

<select id="sel">
  <option value="1">Cat</option>
  <option value="2">Dog</option>
  <option value="3">Fish</option>
</select>

to add the select element.

Then we write

document.getElementById("sel").value = "2";

to set the selection option to the 2nd option by selecting the select drop down with getElementById and setting its value to `’2′.

Conclusion

To change the selected option of an HTML select element with JavaScript, we can set the value property of the select element.

Categories
JavaScript Answers

How to cap a number to a segment with JavaScript?

Sometimes, we want to cap a number to a segment with JavaScript.

In this article, we’ll look at how to cap a number to a segment with JavaScript.

How to cap a number to a segment with JavaScript?

To cap a number to a segment with JavaScript, we can use the Math.max and Math.min methods.

For instance, we write

const n = Math.max(min, Math.min(number, max));

to use Math.min(number, max) to get the min of number and max.

And then we call Math.amx with min and the number returned by Math.min(number, max) to cap the number between min and max.

If number is less than max and bigger than min, then it’ll be returned.

Conclusion

To cap a number to a segment with JavaScript, we can use the Math.max and Math.min methods.

Categories
JavaScript Answers

How to check if a key exists with HTML5 LocalStorage and JavaScript?

Sometimes, we want to check if a key exists with HTML5 LocalStorage and JavaScript.

In this article, we’ll look at how to check if a key exists with HTML5 LocalStorage and JavaScript.

How to check if a key exists with HTML5 LocalStorage and JavaScript?

To check if a key exists with HTML5 LocalStorage and JavaScript, we can use the getItem method.

For instance, we write

if (localStorage.getItem("username") === null) {
  //...
}

to check if the entry with key 'username' exists or not with getItem.

If it returns null, then the entry doesn’t exist.

Conclusion

To check if a key exists with HTML5 LocalStorage and JavaScript, we can use the getItem method.