Search code examples
c++node.jsv8

Google V8 JavaScript Engine - How to set a value to null?


Using the V8 engine, how do I set a value to null? Basically I want to return a variable from a C++ addon to JavaScript, and the variable needs to be set to null under certain conditions.


Solution

  • You can explicitly return null via v8::Null:

    return scope.Close( Null() );
    

    Also, it turns out that if a Value variable is declared, it is automatically assigned to undefined. For example, the following returns undefined back to JavaScript:

     HandleScope scope;
     Local<Value> result;
     ...
    
     return scope.Close(result);