Functions, Scope, and Conditionals in JavaScript for Beginners

Functions, Scope, and Conditionals in JavaScript for Beginners

Learning JavaScript can become more easy and simple when we tend our self to understand it in the depth. Yes, it’s true. For beginners it seems tough at the starting point but practice makes a man perfect. The more you go in its depth the more you understand it. JavaScript is one of the 3 main languages you should know to become a professional Web designer and developer. In this blog we are going to discuss about the functions, scope and conditionals in JavaScript in a very easy way.

Topics to be cover in this blog are:

  • Functions in JavaScript
  • Syntax of JavaScript Function
  • Types of Functions
  • Scope in JavaScript
  • Conditionals

Functions in JavaScript:

In JavaScript a function is executed when it is called or invoked. Functions helps in organizing code and helps in improving the reusability. Functions acts as a wrapper or as a block of code. We define the JavaScript function with a keyword ‘Function’ followed by a name and parentheses (). And the code which is executed by the function is placed inside curly brackets {}.

Syntax of JavaScript Function:

In JavaScript Function names could be letter, digit, underscore, and dollar sign. The code which is executed by function is placed under curly brackets {}.

function name(parameters,……..2,……3….)
{
    Code….
}

Example:

function welcome()
{
    alert(‘Welcome to JavaScript’);
}

Types of Functions:

  • Named Functions
  • Anonymous Functions
  • Parametric Functions
  • Nested Functions

Named Functions:
These are those functions which are declared by name:

Example:

function myFunction()
{
   alert(‘Hey There! How are u…’);
}

Anonymous Functions:
These functions are not declared by name rather these are declared by using var:

Example:

var myFunction = function ()
{
    alert(‘Have u ever visited Delhi’);
}

Parametric Functions:
These are those functions which uses ‘return’ keyword:

Example:

function myFunction(a, b)
{
   return a*b;
}

The result will be: 28

When the JavaScript reaches a return statement, there the function stop executing.
Note: Don’t write anything after the return statement because nothing will be read after that if you do so.

Nested Functions
This is that type of function in which there are many functions inside another function. The main function is called as parent function and the function inside it is called child function.

Example:

function newYear()
{
   function wishes()
   {
       alert(‘Wish u a happy new year’);
   }
   wishes();
}
newYear();

Scope in JavaScript:

The scope of JavaScript is of two types:

  • Local scope
  • Global scope

The scope of a function defines its visibility.

Local Scope:
These variables have local scope. These can only be accessed within function:

Example:

function myFunction()
{
   var oilName = ”Bajaj Almonds Drops”
   // here we can access oilName variable
}
   // here we can’t access oilName variable as it is declared inside a function so local.

Global Scope:
When we declare a variable outside the function then it is called as global variable. All the functions and scripts can access this variable on the web page. Same is also in case of functions.

Example:

var oilName= “Bajaj Almonds Drops”;
alert(oilName);// here we can access oilName as it is a global variable
function myFunction()
{
    alert(oilName);//here also we can access oilName for same reason
}
myFunction();

Conditionals in JavaScript:

In JavaScript we use these conditions to perform different kinds of actions on different stituation.
There are four types of conditional statements in JavaScript.

  • If Statement
  • Else Statement
  • Else if Statement
  • Switch Statement

If Statement:
We use ‘If’ statement when the code is to be executed, if the condition is to be true.

Example:

if(hour<12)
{
    wishes = ”Good Morning”;
}

Else Statement:
We use ‘else’ statement when the condition is false and then the code is to be executed.

Example:

if(hour<12)
{
    wishes= ‘Good Morning’;
}else{
    wishes= ‘Good Noon’;
}

Else If Statement:
We use the ‘Else If Statement’ to specify a new condition when the previous statement is false.

Example:

if(hour<12){
    wishes=’Good Morning’;
}else if(hour<3)
{
    wishes =’Good Noon’;
}else {
    wishes=’Good Evening’;
}

Switch Statement:
In Switch Case the value of the expression is compared with the value of each case and if it matches then the associated code is executed.

Example:

var getDay = prompt(“Enter a number between 0-6 );
switch(getDay)
{
    case 0:
        alert(”Monday”);
    break;
    case 1:
        alert(”Tuesday”);
    Break;
    case 2:
        alert(”Wednesday”);
    break;
    case 3:
        alert(”Thursday”);
    break;
    case 4:
        alert(”Friday”);
    break;
    case 5:
        alert(”Saturday”);
    break;
    case 6:
        alert(”Sunday”);
    break;
default:
        alert( ‘Govt. holiday’);
    break;
}

So, we have reached at the end of this blog, I am a student of Web Design and Web Development Course in this institute. I got this topic to express my views while learning JavaScript from my instructor so I have shared my knowledge with you about functions, scopes and conditional statements in JavaScript. You can apply above concepts according to your requirement. I hope you understand all the mentioned topics very well. If you want to give any suggestions and feedbacks please feel free to write them in given below comment section.

Leave a Reply

You must be logged in to post a comment.

Copy link