Javascript Syntax
__main__ Python module |
code Python module |
COM PHP module |
cURL PHP module |
dis Python module |
Ev PHP module |
http Python module |
imp Python module |
io Python module |
JavaScript |
keyword Python module |
Network PHP module |
numbers Python module |
os Python module |
PS PHP module |
Python functions |
re Python module |
sep |
StackOverflow |
stat Python module |
struct Python module |
UI PHP module
Michael Zippo
04.11.2021
👻 Check our latest review to choose the best laptop for Machine Learning engineers and Deep learning tasks!
If you’re looking to learn how to write JavaScript, you’ve come to the right place.
Each programming language has its own rules, just like English. Think about it. When you were in elementary school, you learned grammar rules to structure sentences.
Much like English, programming languages ​​follow rules to ensure that everyone can understand what is being said. Imagine if we could all create our own rules when using English. No one could communicate. Likewise, programming languages ​​are strict on the rules so that the code you write can be executed.
In this guide, we’ll cover JavaScript syntax. Syntax refers to the set of rules that define how code is written in JavaScript.
Why is syntax important?
In JavaScript, you must follow certain syntax rules. For example, if you don’t close a hook after opening one, an error will be returned. The computer cannot continue to run your program because it does not understand what you are telling it to do.
This deserves an important distinction between computers and humans: while humans may be able to identify the occasional error and skip it, computers cannot do the same when executing code in any programming language. This is why it is important to write code using the syntax of the programming language you are working with.
The syntax also makes your code more readable. When everyone uses the same syntax, it is easy to interpret each other’s programs. Just like in English, once you know the basic rules there are no more lines of code that you cannot read.
In this guide, we’ll focus on the following syntax features:
- Whitespace
- Naming of variables
- Withdrawal
- Comments
- Semicolon
Let’s go!
Whitespace
Programmers are constantly discussing whitespaces. It is a very controversial subject. Putting these debates aside for a minute, there is one key rule you must remember when it comes to JavaScript syntax: you should have spaces between variables and before and after brackets
Suppose you write a loop for
:
This loop prints all numbers between 0 and 4. as you can see it There is a space between our
and the support for opening our
loop. There is a space between our closing brace (() and the opening brace ({). There is also a space between all statements in parentheses:
When assigning a variable outside of a
loop, you will use the same approach:
var biscuit = "raspberry Chocolate Chip";
Adding spaces between yo Our variables make it easier to read code creation.
Unlike other programming languages, you can write a single line JavaScript program. Not a good idea - think how hard it would be to read your code - but you can still do it.
Any statement that is inside a block of code - such as a conditional if
statement or a class - must be indented. Indent refers to the add two spaces, four spaces or a tab at the beginning t code. There is a lot of debate among programmers as to which is best, but as long as you keep your code consistent, you won’t have a problem.
Here is an indented for
loop:
You can see that the term console.log ()
has been indented with a tab. This is because it is contained in our braces. This helps distinguish the content of our for loop from the rest of our code. If we had another block of code in our code, like an if
statement, we would indent its contents further:
This code prints the value of i
if it is equal to three. Otherwise nothing happens. In this snippet, our if
statement is in removal of a tab because it is in our for
loop. Our console.log ()
statement is indented by two tabs because it is contained in our statement if
, which in turn is contained in our for
statement.
Comments
Comments are statements written by the developers, for the developers. technically, they are read by JavaScript, but will not be executed.
Lo The purpose of comments is to help developers keep track of their code. If you are implementing a complex function, you can write some comments so that you know what each part of the function does. Comments are often used in collaborative projects as although every developer can understand a program, even if they haven’t written themselves
in JavaScript, comments are written using double slashes , followed by the comment: ..
All subsequent characters after the double slash are treated as a comment. To create a multiline comment, you can use this syntax:
Any text between / * and * / will be ignored by the JavaScript compiler.
Naming variables
There are many different ways to name a variable. The three most common methods use upper and lower case letters and underscores.
In JavaScript, most developers prefer to use lowercase letters to name variables:
firstName, surname, isAdmin
The variable declaration looks like JavaScript like this:
var firstname = "Tony";
we follow the type of variable you use (in this case var) by the name of the variable, then by an equal sign, then by the value you want to store in the variable.
The first word of a variable must start with a lowercase letter, even if the variable is only one word long. The second word and all subsequent words must start with an uppercase letter, as you can see above.
Although you can write variables using other methods (such as "first_name" in underscore or "FirstName" in uppercase camel), it is not common to do so. in JavaScript.
Variables in JavaScript are case sensitive . this means that firstname
and firstname
, while containing the same basic characters, would be treated differently.
There are a few words called "reserved words‚" that you cannot use as variable names . these are words they have one. special function in JavaScript for example, you cannot name a variable "class" or "for" because they are already used in language
> You can find a list of reserved JavaScript keywords on target Mozilla Developer Network .
Semicolon
In English, each sentence ends with a period ; a point. tells you when to stop reading one sentence and start another. If you think back to your elementary school years, you’ve probably been told that sentences are a place to pause and catch your breath.
Computer programs. don’t "don’t need to catch your breath, but they do need to know how instructions are broken down into a program in JavaScript, most instructions end with a semicolon:
var cookie = "Chocolate fondant"
It is not necessary to specify a semicolon after each declaration block . These are statements like class, switch, do, if, and for. These statements use curly braces to store their code:
In this code, you can see that we ended our return
statement with a semicolon. It is because it is in our function. Our function does not end with a semicolon.
Conclusion
There are many rules in the JavaScript programming language. Each of these rules ensures that you are writing code that you and the browser can read. If you are working in a team, it is especially important to follow the rules of JavaScript syntax.
Here is a summary of the main rules we covered:
- All statements in a variable declaration or after a keyword such as "for" or "if" must be separated by spaces.
- Statements enclosed in a block of code (eg "if", "Else") must be indented.
- Variable names must use lowercase camel and must not be the same as a reserved keyword.
- any declaration outside of block declarations, must end with a comma.
- // can be used to write a comment a single line and / * * / can be used to write a comment spanning several lines.
You are now ready to start coding in JavaScript as a professional developer !
👻 Read also: what is the best laptop for engineering students?
We hope this article has helped you to resolve the problem. Apart from Javascript Syntax, check other __main__ Python module-related topics.
Want to excel in Python? See our review of the best Python online courses 2023. If you are interested in Data Science, check also how to learn programming in R.
By the way, this material is also available in other languages:
Dmitry Emmerson
Munchen | 2023-01-31
Thanks for explaining! I was stuck with Javascript Syntax for some hours, finally got it done 🤗. I am just not quite sure it is the best method
Xu Ungerschaft
San Francisco | 2023-01-31
Thanks for explaining! I was stuck with Javascript Syntax for some hours, finally got it done 🤗. Checked yesterday, it works!
Javier Krasiko
Texas | 2023-01-31
I was preparing for my coding interview, thanks for clarifying this - Javascript Syntax in Python is not the simplest one. Will use it in my bachelor thesis
Shop
Learn programming in R: courses
$FREE
Best Python online courses for 2022
$FREE
Best laptop for Fortnite
$399+
Best laptop for Excel
$
Best laptop for Solidworks
$399+
Best laptop for Roblox
$399+
Best computer for crypto mining
$499+
Best laptop for Sims 4
$
Latest questions
PythonStackOverflow
Common xlabel/ylabel for matplotlib subplots
1947 answers
PythonStackOverflow
Check if one list is a subset of another in Python
1173 answers
PythonStackOverflow
How to specify multiple return types using type-hints
1002 answers
PythonStackOverflow
Printing words vertically in Python
909 answers
PythonStackOverflow
Python Extract words from a given string
798 answers
PythonStackOverflow
Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?
606 answers
PythonStackOverflow
Python os.path.join () method
384 answers
PythonStackOverflow
Flake8: Ignore specific warning for entire file
360 answers
Wiki
Python | How to copy data from one Excel sheet to another
Common xlabel/ylabel for matplotlib subplots
Check if one list is a subset of another in Python
How to specify multiple return types using type-hints
Printing words vertically in Python
Python Extract words from a given string
Cyclic redundancy check in Python
Finding mean, median, mode in Python without libraries
Python add suffix / add prefix to strings in a list
Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?
Python - Move item to the end of the list
Python - Print list vertically