JavaScript | Syntax

JavaScript is the lightweight and dynamic computer programming language. It is used to create client side dynamic pages. It is open source and cross-platform language.

Basic Syntax:

filter_none

edit
close

play_arrow

link
brightness_4
code

<script>  
document.write("Basic Print method in JavaScript");  
</script>  

chevron_right


JavaScript syntax refers to the set of rules that determines how JavaScript programs are constructed:

// Variable declaration
var c, d, e;

// Assign value to the variable
c = 5; 

// Computer value of variables
d = c;
e = c/d;

JavaScript Variables: A JavaScript variable is the simple name of storage location where data to be stored. There are two types of variables in JavaScript which are listed below:

  • Local variables: Declare a variable inside of block or function.
  • Global variables: Declare a variable outside function or with window object.

Example:



filter_none

edit
close

play_arrow

link
brightness_4
code

<script>
  
// Declare a variable and initialize it
// Gloabal variable declaration
var Name="Apple";   
  
// Function definition
function MyFunction() {  
      
    // Local variable declaration
    var num = 45;    
      
    // Display the value of Gloabal variable
    document.writeln(Name);
      
    // Display the value of local variable
    document.writeln("<br>" + num );
}  
  
// Function call
MyFunction();
   
</script>

chevron_right


Output:

Apple
45

JavaScript Operator: JavaScript operators are symbols that are used to compute the value or in other word we can perform operations on operands. Arithmetic operators ( +, -, *, / ) are used to compute the value and Assignment operator ( =, +=, %= ) are used to assign the values to variables.

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

<script> 
  
// Variable Declarations
var x, y, sum;
  
// Assign value to the variables
x = 3;
y = 23;
  
// Use arithmetic operator to
// add two numbers
sum = x + y;
  
document.write(sum);
  
</script>

chevron_right


Output:

26

JavaScript Expression: Expression is the combination of values, operators and variables. It is used to compute the values.

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

<script> 
  
// Variable Declarations
var x, num, sum;
  
// Assign value to the variables
x = 20;
y = 30
  
// Expression to divide a number
num = x / 2;
  
// Expression to add two numbers
sum = x + y;
  
document.write(num + "<br>" + sum);
  
</script>

chevron_right


Output:

10
50

JavaScript Keyword: The keywords are the reserved words that have special meaning in JavaScript.

// var is the keyword used to define the variable
var a, b;

// function is the keyword which tells the browser to create a function
function GFG(){};

JavaScript Comments: The comments are ignored by JavaScript compiler. It increase the readability of code. It adds suggestions, Information and warning of code. Anything written after double slashes // (single line comment) or between /* and */ (multi line comment) is treated as comment and ignored by JavaScript compiler.

Example:



filter_none

edit
close

play_arrow

link
brightness_4
code

<script> 
  
// Variable Declarations
var x, num, sum;
  
// Assign value to the variables
x = 20;
y = 30
  
/* Expression to add two numbers */
sum = x + y;
  
document.write(sum);
  
</script>

chevron_right


50

JavaScript Data Types: JavaScript provides different datatype to hold different values on variable. JavaScript is a dynamic programming language, it means do not need to specify the type of variable. There are two types of data types in JavaScript.

  • Primitive data type
  • Non-primitive (reference) data type
// It store string data type
var txt = "GeeksforGeeks";

// It store integer data type
var a = 5;
var b = 5;

// It store Boolean data type
(a == b )
 
// It store array data type
var places= ["GFG", "Computer", "Hello"];

// It store object data
var Stdent = {firstName:"Johnny", lastName:"Diaz", age:35, mark:"blueEYE"}

JavaScript Functions: JavaScript functions are the blocks of code used to perform some particular operations. JavaScript function is executed when something call it. It calls many times so the function is reusable.

Syntax:

function functionName( par1, par2, ....., parn ) {  
    // Function code
}  

JavaScript function can contain zero or more arguments.

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

<script>
    
// Function definition
function func() {  
      
    // Declare a variable
    var num = 45;                  
      
    // Dispaly the result
    document.writeln(num);     
}  
  
// Function call
func();
  
</script>

chevron_right


Output:

45


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.