There is an extension EXT_frag_depth
Since this is an extension, it may not be available everywhere, so you need to check its existence.
var isFragDepthAvailable = gl.getExtension("EXT_frag_depth");
If isFragDepthAvailable not false, you can enable it in your shaders with
#extension GL_EXT_frag_depth : enable
Otherwise, you can manipulate gl_Position.z in your vertex shader, although I suspect that this is not a really viable solution for most needs.
source share