fix js issue that caused undefined behavior in some occasions