If you’re new to programming and would like to learn the JavaScript basic syntax, JavaScript is one of the most popular languages on the web today. It is a powerful scripting language that helps developers create dynamic and interactive websites.
This article will provide a brief introduction to the basic syntax of JavaScript, and will help you to understand the basics of this popular language. Hopefully, this article will give you a good starting point if you are new to JavaScript or if you need to brush up on your skills.
Table of Contents
What is Syntax?
Syntax is the structure of a programming language, and it determines how statements are laid out in a program. It includes rules that determine which symbols can be used where, what order they must appear in, and what kind of constructions (if any) can be used. Knowing the syntax of a programming language can help you to write more efficient and correct code.
Script Argument
JavaScript statements may be put inside the <script>… </script> HTML tags on a website to implement it. You may put the JavaScript code anywhere on your page, as long as it’s inside the <script> tags, but it is typically recommended to keep it inside the <head> tags. The browser program is alerted to start interpreting all of the text between these tags as a script by using the <script> tag.
First JavaScript Code
<html>
<body>
<script>
document.write("Welcome to my first code");
</script>
</body>
</html>
White Space and Line Break
It’s important to use white space and line break correctly. This allows your code to be read more easily and helps avoid errors. When using indentation, spaces should always go on the left side of the colon (;). Code that is indented two levels or more will then need a line break after the first level of indention.
<html>
<body>
<script>
document.write("Welcome to my first code");
document.write("Welcome to codeleaks");
</script>
</body>
</html>
Semicolon
A semicolon is used to separate two lines of code or simple statements in a javascript file. It can be useful for indicating that one line of code has finished executing, and the next line should begin execution there. It is not mandatory but is a good programming practice to use semicolon.
<html>
<body>
<script>
document.write("Welcome to my first code");
//semicolon to terminate
</script>
</body>
</html>
Case Sensitivity
Case sensitivity is important in javascript. Unlike other languages where keywords are always lowercase, javascript allows you to have uppercase and lowercase versions of the same word. This can be helpful when using variables or constants because they will be treated as expected based on their casing. For example, JavaScript and javascript are not same because they are of different cases.
Comments in JavaScript
Comments() is a built-in function in javascript that allows you to add comments to your code. Comments are simply text strings that serve as notes for yourself or other developers while working on a project. They can be helpful for documenting how certain parts of your code work, noting changes you’ve made, or just providing general feedback. To use this function, include the following at the beginning of your script: // Comments begin here, there are two types of comments in javascript:
- Single-lineĀ
- Multi-Line
1.Single-Line Comments
JavaScript allows you to add a single line of comments in the same spot where you would normally type a code block. This is particularly useful when you want to flag or document specific lines of code. To use this feature, place // at the beginning of your comment and // at the end.
<html>
<body>
<script>
document.write("Welcome to my first code");
//print hello message//
</script>
</body>
</html>
2.Multi-Line Comments
One way to block comments in your JavaScript code is to use the comment syntax /* */. This will allow you to add comments but block all other code from being executed within that section of the document.
<html>
<body>
<script>
document.write("Welcome to my first code");
/*print
hello
message*/
</script>
</body>
</html>
JavaScript Variables
JavaScript variables can be used to store any type of information, including data that is retrieved from a web page. This makes them ideal for storing small amounts of data or retrieving specific pieces of information multiple times.
<script>
var code="welcome to codeleaks";
</script>
There are two types of Variables in JavaScript Programming language:
1.Local Variable
Javascript local variables are a great way to store temporary data in your scripts. This can be useful for storing information that you need within the context of a single script, without having to globalize it or create an external variable.
2.Global Variable
They allow you to store data across different parts of your codebase without having to repetitively type the same information twice
JavaScript Operators
There are six main operators that you need to be aware of when working with Javascript: assignment (=), addition (+), subtraction (-), multiplication (*), division (/) and logical AND (&). These operators can be used in conjunction with each other, as well as with the parentheses symbol (), to create more complex expressions.
<script>
var x, y, sum, sub, mul, div;
x = 3;
y = 23;
sum = x + y;
sub = x - y;
mul = x * y;
div = x / y;
document.write(sum);
document.write(sub);
document.write(mul);
document.write(div);
</script>
JavaScript Expressions
Expressions are a powerful tool that allows you to manipulate the values of variables in your code, making it easier to write more efficient and effective code. They can be used for many different purposes, including conditionals, loops, and adding new functionality to your applications.
<script>
var x, y, sum, sub;
x = 3;
y = 23;
sum = x + y; //expression to add
sub = x - y; //expression to sub
document.write(sum);
document.write(sub);
</script>
JavaScript Keywords
In a programming language, keywords are symbols that designate specific identifiers and operations. They can identify variables, functions, or other elements of a code. Keywords also serve as labels for data items so the programmer can more easily access them. Here are some of the common keywords listed that are used in JavaScript:
var | else | for |
continue | catch | case |
default | in | new |
void | function | do |
switch | return | finally |
class | false | instanceof |
null | width | if |
while | break | try |
debugger | this | true | typeof | throw | delete |
JavaScript DataType
A datatype is an object that provides a specific set of features when used in programming. Javascript has several built-in datatypes, including numbers, strings, Booleans (true or false), dates and times, arrays (of any type), objects (which can be null or have properties defined by the programmer), and functions.
1.String
In JavaScript, you can print a string with both a single and double quotation.
<script>
var code="welcome to codeleaks";
var code='welcome to codeleaks';
</script>
2.Number
<script>
var number=67;
var float= 8.9;
</script>
3.Boolean
<script>
var bool="true";
var bool="false" ;
</script>
JavaScript Functions
A function is a block of code that performs a specific task. Whenever you need to execute the same set of instructions multiple times, or when you want to reuse some code for different purposes, Functions are the perfect solution.
function add(x,y)
{
let result = x * y;
return result;
}
JavaScript Identifiers
JavaScript identifiers are unique strings that can be used to uniquely identify variables, functions, and properties in a JavaScript codebase. This allows for more readable and organised codebases, as well as increased reliability and consistency. IDs should always start with an alphanumeric character (a-zA-Z0-9) and must be at least four characters long.
JavaScript Statements
Statements are a group of code instructions that define how your program will behave. They can be used to initialize variables, conditionally execute code based on user input, and more. Statements are typically placed inside curly braces ({}) but can also appear anywhere in the source code. Statements should be concise so you do not waste valuable computer resources typing unnecessary words or multiple lines of code.
To know more about JavaScript in detail visit the article, Introduction to JavaScript.
FAQs
What is the main characteristic of a syntax error?
A syntax error occurs when the rules of grammar are not followed. This can make it difficult for a computer to understand what you are trying to say, which can lead to an error message or even the entire page being simply crashed
Is JavaScript Object Oriented?
JavaScript is considered to be object-oriented, but there are a few key aspects of the language that make it unique. For one, JavaScript doesn’t have classes or inheritance like other paradigms do.
Conclusion
Well, that’s all we have in store for you today. We hope that this article has helped you understand the JavaScript basic syntax and structure better. Now all you need to do is try out this language in your projects and see what it can do. Thanks for reading.