0

A colleague and I were discussing the implementation of the JavaScript "if" statement and I wanted to find the implementation so we could read through it and hopefully get a bit wiser.

However, I wasn´t able to find it anywhere, so my question is; Where do you go to read the JavaScript source code?

4
  • if is not a function, it's a statement defined in the javascript spec and implemented by the engine like blink, so you could look for the code of a particular engine like blink Commented Jun 30, 2022 at 12:24
  • 2
    There are numerous JavaScript compilers available, all with different source code, some of which is open and some of which is not. Questions seeking recommendations for off-site resources are explicitly off-topic on Stackoverflow. Commented Jun 30, 2022 at 12:25
  • 1
    I think it is a good question, no need to downvote it. The answer to this question and discussion can help people understand the language and the surrounding ecosystem more. Commented Jun 30, 2022 at 12:26
  • 1
    @kodeaben please see edits to my answer Commented Jun 30, 2022 at 12:36

1 Answer 1

1

if is not a function but is a keyword. There is no JavaScript implementation of it.

The compiler/transpiler understands the if statement just like it understands other tokens from the var, const, and let to do {} while(); and for. All of these are keywords with special meaning known to the JavaScript interpreter.

You would want to look the implementation of the interpreter/compiler and runtime implementation to understand how it handles the various control keywords such as if. Alternatively you can look at the specs of the language.

Edit:

It is worth noting that JavaScript is ECMAScript. There are ECMAScript standards and JavaScript implements these. So concepts such as truthiness and falsiness (whether a value is truthy of falsey) is in the ECMAScript standards.

Particularly, you can check section 13.6.7 that speaks to the semantics of the if statement: https://262.ecma-international.org/11.0/#sec-if-statement-runtime-semantics-evaluation

From there, one can find what it means for something to be truthy/falsey, which is ToBoolean, as defined in the specs (not a JavaScript function):

enter image description here

Sign up to request clarification or add additional context in comments.

2 Comments

"There is no JavaScript implementation of it." — That's flat out wrong. There are several JavaScript runtimes written in JS. The question isn't asking for a the if source code to be written in JS anyway.
@Quentin There is no JavaScript if statement implemented in some JavaScript library that your JavaScript code references. That's what I meant to say. I didn't say that a JavaScript runtime/interpreter etc implementation cannot be implemented in a general language such as JavaScript.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.