Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Tadjiev Codes
Tadjiev Codes
9,626 Points

What are the Void Functions in JavaScript? Does anyone know? Thanks

What are the Void Functions in JavaScript? Does anyone know? Thanks

2 Answers

Steven Parker
Steven Parker
229,644 Points

Where did you see this term? JavaScript doesn't implement a "void" type, but that term is used in other languages to declare functions that don't return any value.

In JavaScript, attempting to use a return value from a function that doesn't provide one does not cause an error but gives you "undefined" as the value.

Tadjiev Codes
Tadjiev Codes
9,626 Points

Hi, I think you're right. Mostly I haven't found anything about this online beside this one here... Is it Javascript related?The link as follows === https://www.w3resource.com/javascript/operators/void.php Description The void operator is used to evaluate a JavaScript expression without returning a value.

Syntax

void (expression) void expression Example -1:

The following web document shows how the void operator is used.

HTML Code

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset=utf-8>
<title>JavaScript void operator example</title>
</head>
<body>
<a href="javascript:void(alert('Thank you.'))">Click here to see a message</a>
</body>
</html>
Steven Parker
Steven Parker
229,644 Points

I'd never run into that before, but sure enough, JavaScript has a "void" operator!

I sure can't think of any reason to use it, though. If you actually need an undefined value, you can use the keyword "undefined", and if you want to evaluate an expression but don't care about the result, you can simply ignore it.