The javascript basic syntax is like C or other C Languages like Java, C# or Perl. If you have known those languages before, you might find javascript syntax is easy to understand. Below are the basic characteristics of Javascript script syntax you should know.
Case Sensitive
Javascript syntax is case sensitive, any variables, function names or operators you use are all case sensitive. If you declare a variable called myNumber, it will have a different meaning if you use MyNumber instead.
Identifiers
Identifiers can be considered as the name of a property, it can be a declared variable, function name or function argument. Note: the first character of identifiers has to be started with a letter or underscore or a dollar sign $ character, other than those characters are not allowed for ex: numbers.
Keywords
The following keywords are reserved by Javascript syntax language. You cannot declare a variable or function name using the following keywords. They are used for performing specific operations or statements syntax.
- break
- case
- catch
- continue
- debugger
- default
- delete
- do
- else
- finally
- for
- function
- if
- in
- instanceof
- new
- return
- switch
- this
- throw
- try
- typeof
- var
- void
- while
- with
Reserved Words
The following reserved words are used by Javascript syntax language. You cannot declare a variable or function name using the following reserved words. They are used for identifiers and property names.
- abstract
- boolean
- byte
- char
- class
- const
- debugger
- double
- enum
- export
- extends
- final
- float
- goto
- implements
- import
- int
- interface
- long
- native
- package
- private
- protected
- public
- short
- static
- super
- synchronized
- throws
- transient
- volatile
Declare a variable in Javascript
To declare a variable in javascript is simple, what you need to do is using a keyword var at the front. See below example.
//this is how you declare a variable var myNumber = 10; //alternatively, you var is optional to use, but it is TRUELY recommended that you use this one as a good common programming practice. This variable will work as well. myNumber = 10;
In javascript language, when you declare a variable, it can be considered flexible because you can declare a number, string, boolean value without has to specify the type of the object. See below example.
//number value var myNumber = 10; //string value var myName = "My Name"; //boolean value var flag = false;
Writing your first Javascript function.
You will find the basic syntax on how to write your first javascript function//function to popup message box with text Hello Javascript function popupHello(){ alert("Hello Javascript"); } //function that will accept two parameters and will return the total sum of those values function sumUp(value1, value2){ return value1 + value2; }
How to comment in Javascript
You can comment the javascript using two methods. One is to comment one single line. You can use double forward slash characters
//var myNumber = 0;
If you have more than one line that you want to comment, you can using the forward slash asterisk and ends the comment with the asterisk forward slash characters. See example below.
/* var myNumber = 0; var myName = "Hello Javascript"; */