diff --git a/dlls/jscript/lex.c b/dlls/jscript/lex.c
index 8d8e2d9fd4fc7deee80291c001858b24abe1e21b..5e0226a41e1d0669a85a85e442cc6cd19f0dd8eb 100644
--- a/dlls/jscript/lex.c
+++ b/dlls/jscript/lex.c
@@ -231,6 +231,16 @@ static BOOL skip_comment(parser_ctx_t *ctx)
     return TRUE;
 }
 
+static BOOL skip_spaces(parser_ctx_t *ctx)
+{
+    while(ctx->ptr < ctx->end && isspaceW(*ctx->ptr)) {
+        if(is_endline(*ctx->ptr++))
+            ctx->nl = TRUE;
+    }
+
+    return ctx->ptr != ctx->end;
+}
+
 static BOOL unescape(WCHAR *str)
 {
     WCHAR *pd, *p, c;
@@ -534,11 +544,7 @@ static BOOL parse_numeric_literal(parser_ctx_t *ctx, double *ret)
 static int next_token(parser_ctx_t *ctx, void *lval)
 {
     do {
-        while(ctx->ptr < ctx->end && isspaceW(*ctx->ptr)) {
-            if(is_endline(*ctx->ptr++))
-                ctx->nl = TRUE;
-        }
-        if(ctx->ptr == ctx->end)
+        if(!skip_spaces(ctx))
             return tEOF;
     }while(skip_comment(ctx) || skip_html_comment(ctx));