Code Syntax – How to define Variables and create function.

Please follow & like us :)

You can use “var”, “let” and “const” keyword to declare the variable

Prior to the ECMAScript 2015 (ES6), we were using “var” keyword and with ES6 we are using “let” – “const” keywords.

As we are coding with ES6, we will use “let” or “const” to declare the variable. But you must aware about ‘var’ keyword as its still widely used in some of old websites.

Now once you declare the variable, you need to define the value.

Here are the data types with examples which you can use to define the values.

Variables can be String, Number, Boolean, Array or Object.

Here are the examples:

String:

let user = “admin”;

Number:

let midNum = 24;

Boolean:

let check = true;

Array:

let usrarr = [‘test’, ‘test1’, ‘test3’];

Object:

 let usrobj = {
 
  p1: 10,

   p3: 20

 }; 

You can use ‘function’ keyword to declare function.

function helloWorld() {
  return alert(‘hello world’);
} 

When you call helloWorld() -> it will return that alert!

Now you can also pass arguments in function like this:

function helloWorld(usr) {
  return alert(usr);
} 

When you call helloWorld(“test”); it will return alert with ‘test’ as text in it. So now we have generic idea how to structure Javascript code.