What precisely is a construct in JavaScript?
Asked Answered
T

3

6

As I learn JavaScript I've been looking around the web and seen numerous references to constructs in Javascript, but I can't seem to find a complete definition of what they are and what they are not, especially in the context of Javascript.

For instance, in 'Similar Questions' I see links that lead to an example featuring the following code:

In What is this construct in javascript?:

(function () {

})();

From what I understand this is a construct, but what are they defined by?

Thomas answered 3/9, 2012 at 5:30 Comment(2)
@dbaseman, read the question more carefullySybil
@u1sonderzug, Is there anything you are still confused about here?Sybil
S
13

Construct is a generic term referring to an arbitrary aggregate of code in a specific formation. It is not a javascript-specific term.

Basically, it can apply to anything. So, while the code you referenced is a construct known as a self invoking anonymous function, var x = "hello world"; is a construct known as a variable declaration and assignment.

Sybil answered 3/9, 2012 at 5:36 Comment(5)
In JavaScript, could it be said that anything that ends in a ; is a construct? Up until the ; ?Thomas
Anything is a construct. A keyword is a construct. A value literal is a construct. The semicolon is not necessary. You are overthinking this.Sybil
But if var x = "hello world"; is a construct, based on what you said that 'Anything is a construct' does it mean that 'x' or just 'world' is a construct? Doesn't it refer to a line of code somehow? I know this probably sounds ridiculous - but I really am not 100% clear here.Thomas
Yes. x is a variable. The "world" is a string literal. Variables and string literals are both constructs in javascript. The definition of construct in terms of javascript is the same definition you will find in a standard english dictionarySybil
"Statement" is the term for the language constructs that end with ;.Cashier
C
6

A "language construct" is the full term you're looking for. According to the linked definition:

A language construct is a syntactically allowable part of a program that may be formed from one or more lexical tokens in accordance with the rules of a programming language.

So it's any valid segment of written code that follows the rules of the language. It's a generalisation of words like "expression", "statement", "function argument list", "assignment statement", "keyword", "function definition", etc. that each define a series of tokens to look for and what they will mean in the rules of the language. The code of a completed program is constructed with them.

Cashier answered 1/3, 2016 at 19:22 Comment(1)
"It's a generalisation of words like..." is why this is the winner for me. Makes it clear that "construct" is a meta concept, not a coding pattern.Perseid
G
0

From the Scripting Reference in MASTERING HTML4 by Deborah and Eric Ray:

Constructs are the structures that you can use in a JavaScript to control the flow of the script

They go on as to alphabetically list ALL such a controls which include Break, Comment, If, If else.... So a construct is a very specific term definition in JavaScript used to name and encompass all the statements (structures) that control the flow of a script.

Gynarchy answered 19/1, 2017 at 19:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.