Categories
JavaScript Answers

How to get value from object with default value with JavaScript?

Sometimes, we want to get value from object with default value with JavaScript.

In this article, we’ll look at how to get value from object with default value with JavaScript.

How to get value from object with default value with JavaScript?

To get value from object with default value with JavaScript, we can use the null coalescing operator.

For instance, we write

let foo = obj.maybeUndefined ?? "default value";

to use 'default value' as the fallback value for foo if obj.maybeUndefined is null or undefined.

The null coalescing operator is ??.

Conclusion

To get value from object with default value with JavaScript, we can use the null coalescing operator.

Categories
JavaScript Answers

How to call a JavaScript function recursively?

Sometimes, we want to call a JavaScript function recursively.

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

How to call a JavaScript function recursively?

To call a JavaScript function recursively, we call the same function inside the function.

For instance, we write

const factorial = (n) => {
  if (n <= 1) {
    return 1;
  }
  return n * factorial(n - 1);
};

to define the factorial recursive function.

In it, we check if n is less than or equal to 1.

If it is, we return 1.

Otherwise, we return n multiplied by the result of factorial called with n - 1.

factorial is called until n is less than or equal to 1.

Conclusion

To call a JavaScript function recursively, we call the same function inside the function.

Categories
JavaScript Answers

How to convert Markdown to HTML with JavaScript?

Sometimes, we want to convert Markdown to HTML with JavaScript.

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

How to convert Markdown to HTML with JavaScript?

To convert Markdown to HTML with JavaScript, we can use the showdown library.

For instance, we write

<script src="https://cdnjs.cloudflare.com/ajax/libs/showdown/2.1.0/showdown.min.js"></script>

to add the showdown library with a script tag.

Then we write

const converter = new showdown.Converter();
const text = "# hello, markdown!";
const html = converter.makeHtml(text);

to create a showdown.Converter convert object.

Then we call makeHtml on it with the Markdown text to convert it to html.

Conclusion

To convert Markdown to HTML with JavaScript, we can use the showdown library.

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.