What you are experiencing are called, ‘growing pains’. Everyone experiences them to one degree or another. A long list of learners hit the same wall you did once JS came into the picture. Perfectly normal and expected. Don’t give up.
All languages have a set syntax, JS being no different. It actually adopted what was already a well established syntax common to Java, C, and other similar class based language, of which JavaScript (ECMAScript) is not one.
It doesn’t take long for curly braces and semi-colons to become second nature. Think in terms of ‘template’.
console.log("This is a statement.");
const statement = "This is a statement.";
const a = 42;
All of the above have the same thing in common: they are statements. They all end with a semi-colon which tells the parser, ‘end of statement’ and it stops reading-in the line at that point and begins to interpret it.
Curly braces define objects, whether as ‘associative array’ like objects,
b = {
x: 3,
y: 4,
z: 5
}
where each item in the above ‘array’ is a key-value pair, and are separated by commas (the curly braces act as delimiters and define the boundaries of the object); or, they are used to define the body or block of a code segment in one of several constructs used in JS.
if (condition) {
// action statement;
}
The above is a simple if
statement. Note that there is no semi-colon after the curly brace, only the line(s) contained inside the block. Notice the parentheses around the condition? The condition may be any expression (something that resolves to a value) and when we see parens we should not expect to see a statement between them, only ever expressions.
That brings us to functions, which like the above have both parens and curly braces in their syntax.
const foo = function (param) {
// code body statements;
return param;
}
Above, param
can be one or more comma separated variable names (parameters) that will receive a value from the function call, so they are still expressions, as such.
console.log(foo("This is the argument."));
// This is the argument.
Notice how we passed in a value with our call to foo()
and it responded by return
ing it, whereupon it got logged out to the console (the display).
You’re be seeing a lot of these ‘templates’, or patterns in your travels, which is how they will become second nature once the full gravity of their use is apparent. Soon enough you can put the syntax roller coaster behind you and begin to dive into the logic and algorithms of real programming.
Bottom line, expect set backs and confusion. This is perfectly normal and it will get easy with time and continued effort. As long as you are confused, you are learning. Just don’t give up.