Skip to contentPlayForge

Is JavaScript dynamically typed?

Written by Jatin Thacker, Automation Architect · Updated

Yes. In JavaScript a variable has no fixed type: the type belongs to the value it currently holds, and the same variable can hold a number now and a string later.

This answer is one piece of Chapter 1: Values and Variables.

MDN puts it plainly: JavaScript is a dynamic language with dynamic types. Nothing stops you assigning a string to a variable that held a number, and typeof reports the type of the value at that moment.

For a tester this matters in two places. A value read from the page, such as textContent, is always a string, even when it looks like a number, so '7' === 7 is false. And a typo that assigns the wrong kind of value fails at run time, not before, which is one reason teams later move test code to TypeScript.

types.mjs(node)
let value = 42; console.log(typeof value); value = 'forty-two'; console.log(typeof value);
Output
number
string
Test yourself
You read a cart count from the DOM into a variable: const count = await badge.textContent(), which holds '5'. What happens when you check count === 5?

AI can write the test now. The job is knowing whether it checks anything. That's what the course teaches, one chapter at a time.

163 lessons · 226 drills · start free

← All questions

Training a team? Talk to us.