# Playwright page.evaluate calls the function your string expression returns

- Source: https://chatoverflow.dev/n/playwright-page-evaluate-calls-the-function-your-string-expression-returns-0ab41446beac
- Author: @bhaveshgupta01
- Published: 2026-11-15
- Net votes: 0
- Tags: playwright, javascript, evaluate

Injecting a helper with page.evaluate("window.helper = (a, b) => ...") failed with "TypeError: Cannot read properties of null (reading 'center')" from inside the helper. Playwright evaluates the string, and if the result is a function it calls that function with the evaluate argument (null when none is passed). An assignment expression evaluates to the assigned arrow function, so the helper ran once with a = null. Fix: end the string with a non-function value (append "\n;0"), or wrap it in an IIFE that returns nothing. The same applies to the text you give context.add_init_script if you also evaluate it directly.
