JavaScript code can be written inside HTML Script Tags or in a separate file with .js extension.
Write JavaScript code here....
<script>
//Write javascript code here...
</script>
JavaScript uses the unicode character set, so allows almost all characters, punctuations, and symbols.
JavaScript is a case-sensitive scripting language. So, name of functions, variables and keywords are case sensitive. For example, myfunction and MyFunction are different, Name is not equal to nAme, etc.
In JavaScript, a variable is declared with or without the var keyword.
Example:
JavaScript Statements
<script>
var name = "Steve";
id = 10;
</script>
JavaScript statements are separated by a semicolon. However, it is not mandatory to end a statement with a semicolon, but it is recommended.
Example:
<script>
var one = 1; two = 2; three = 3; //three different statements
var four = 4; //single statement
var five = "Five" //single statement without ;
</script>
JavaScript ignores multiple spaces and tabs. The following statements are the same.
Example: Whitespaces in JavaScript
<script>
var one =1;
var one =1;
var one =1;
</script>
A comment is single or multiple lines, which give some information about the current program. Comments are not for execution.
Write comment after double slashes // or write multiple lines of comments between /* and */.
Example: Comment JavaScript Code
<script>
var one =1; // this is a single line comment
/* this
is multi line
comment*/
var two = 2;
var three = 3;
</script>
A string is a text in JavaScript. The text content must be enclosed in double or single quotation marks.
Example:
<script>
var msg = "Hello World" //JavaScript string in double quotes
var msg = 'Hello World' //JavaScript string in single quotes
</script>
JavaScript allows you to work with any type of number like integer, float, hexadecimal etc. Number must NOT be wrapped in quotation marks.
Example: Numbers in JavaScript
<script>
var num = 100;
var flot = 10.5;
</script>
As in other languages, JavaScript also includes true and false as a boolean value.
Example: Booleans in JavaScript
<script>
var yes = true;
var no = false;
</script>
Keywords are reserved words in JavaScript, which cannot be used as variable names or function names.
var | function | if |
else | do | while |
for | switch | break |
continue | return | try |
catch | finally | debugger |
case | class | this |
default | false | true |
in | instanceOf | typeOf |
new | null | throw |
void | width | delete |