---
title: "Check JavaScript syntax in C# without running it"
url: https://recursiveai.org/oneecma/cookbook/check-javascript-syntax-without-running/
description: "Validate JavaScript syntax and early errors in C# without execution. Includes module mode, real diagnostics and a runnable OneEcma example."
version: "1.1.1"
---

# Check JavaScript syntax in C# without running it

**OneEcma** is a free, open-source JavaScript interpreter for .NET from Recursive AI. MIT or Apache-2.0, no native dependencies.

[NuGet](https://www.nuget.org/packages/RecursiveAI.OneEcma) · [GitHub](https://github.com/recursive-ai-oss/oneecma-csharp) · [Docs](https://recursiveai.org/oneecma/docs/) · [Download example (.zip)](https://recursiveai.org/oneecma/cookbook/check-javascript-syntax-without-running/check-javascript-syntax-without-running.zip)

Check JavaScript before you run it: parsing finds syntax and declaration errors without evaluating the script.

```shell
dotnet add package RecursiveAI.OneEcma
```

Requires [.NET 10](https://dotnet.microsoft.com/download/dotnet/10.0) (net10.0); .NET 9 and earlier aren’t supported.

```csharp
using RecursiveAI.OneEcma;

var errors = ScriptEngine.Check("let = ;");
foreach (var error in errors)
    Console.WriteLine(error.Headline);
```

```text
Unexpected token ';'
```

Unzip [the example](https://recursiveai.org/oneecma/cookbook/check-javascript-syntax-without-running/check-javascript-syntax-without-running.zip), then `dotnet run`.

## The full program

The download checks a broken statement, a duplicate declaration and valid code that would change a global and throw if executed. All JavaScript inputs are inline in `Program.cs`.

```csharp
using RecursiveAI.OneEcma;

string[] inputs =
[
    "let = ;",
    "let count = 1; let count = 2;",
    "globalThis.changed = true; throw new Error('not run');"
];
foreach (string source in inputs)
{
    Console.WriteLine(source);
    var errors = ScriptEngine.Check(source);
    Console.WriteLine($"Diagnostics: {errors.Count}");
    foreach (var error in errors)
        Console.WriteLine($"  {error.Position.Line}:{error.Position.Column}: {error.Headline}");
}
```

```text
let = ;
Diagnostics: 1
  1:7: Unexpected token ';'
let count = 1; let count = 2;
Diagnostics: 1
  1:20: Identifier 'count' has already been declared
globalThis.changed = true; throw new Error('not run');
Diagnostics: 0
```

Malformed JavaScript returns diagnostics rather than throwing; cancellation can throw `OperationCanceledException`.

## Variations

**Check a module instead of a classic script.** This standalone snippet accepts an export declaration:

```csharp
using RecursiveAI.OneEcma;
using RecursiveAI.OneEcma.Syntax;

var options = new ParseOptions { Goal = ParseGoal.Module };
var errors = ScriptEngine.Check("export const answer = 42;", options);
Console.WriteLine($"Module diagnostics: {errors.Count}");
```

```text
Module diagnostics: 0
```

## Related recipes

- [Extract JavaScript module imports in C#](https://recursiveai.org/oneecma/cookbook/extract-javascript-module-imports/)
- [Inspect JavaScript lexical scopes in C#](https://recursiveai.org/oneecma/cookbook/inspect-javascript-lexical-scopes/)
- [Limit JavaScript regex execution in C#](https://recursiveai.org/oneecma/cookbook/run-javascript-regex-with-fuel-limit/)

## The short answer

Call `ScriptEngine.Check(source)` to validate JavaScript syntax in C# without execution. It returns diagnostics; an empty collection means no syntax or early errors, such as declaring the same name twice, were found. It does not create a realm or evaluate expressions. Use it before saving an editable script or accepting a module for later processing.

The full example deliberately includes a global assignment followed by a `throw`. Both are valid syntax, so that input has no diagnostics. The assignment never takes effect and the exception never runs.

## How do I check JavaScript syntax in C#?

Pass the source string to `Check`, then display each diagnostic’s `Headline`. For an editor, also keep `PrimarySpan` and `Position`. The span identifies the source range to underline; the position gives its line and column. Keep the original source alongside the diagnostic so offsets still refer to the text the user sees.

A check catches more than misplaced punctuation. The duplicate `let count` declaration in the full example is an early error: the parser records bindings and detects the redeclaration without needing either initializer’s value. Conversely, a call to an unknown function can be syntactically valid. Syntax checking cannot tell you that a host function will exist when the code eventually runs.

For a file-based editor, read the file into a string in your application and pass that string unchanged. `ParseOptions.FileName` controls the name in reports. It is not an instruction to read a file. The [syntax tree documentation](https://recursiveai.org/oneecma/docs/ast/) describes the available parse options and diagnostic behavior.

## Can a JavaScript code checker fix syntax errors?

`Check` reports errors; it does not edit source. OneEcma also exposes an explicit `CheckAndRepair` operation for the documented syntax-only repair table. That is a separate choice, not an automatic side effect of validation. If users own the source, show proposed changes before replacing their text.

Diagnostics can contain help and recovery information. Parser recovery may insert a missing token or skip a stuck token internally to continue finding errors. That recovered tree is not a corrected program. Do not treat it as executable source, and do not infer that a list of diagnostics contains every independent mistake: nearby fallout can be merged.

## Can I use a JavaScript syntax checker for modules?

Yes. Select `ParseGoal.Module`, as in the variation above. The default goal is a classic script; module declarations such as `export` need the module goal. Module parsing is strict and permits top-level `await`. Choose the goal according to how your application will use the source, not according to which mode happens to accept it.

If you need declarations as well as errors, call `JsParser.Parse` and inspect `HasErrors` before consuming the tree. Parse results with errors are best-effort diagnostic trees. Bound deeply nested input with `MaxDepth`, cap diagnostic collection with `MaxErrors`, and pass a cancellation token when an editor request can become obsolete. These are parser controls, not runtime execution limits.
