11 things about JavaScript functions .NET developers must know: Part 1

I have often seen .NET developers struggle with the JavaScript. They try to compare C# functions with the JavaScript functions and usually do some minor but conceptual mistakes while using it. As we all will agree that JavaScript functions are the backbone of the JavaScript programming language, and if one has good understanding of the function then using JavaScript is easier. So in this two parts series, I will focus some important concepts of JavaScript functions.

In this part of the series, we will cover the following six topics:

  1. JavaScript function as an expression
  2. JavaScript function as a statement
  3. Return statement in the JavaScript function
  4. Parameters in the JavaScript function
  5. Arguments object in the JavaScript function
  6. Varargs JavaScript function

JavaScript functions can be an expression

A JavaScript function can be an expression. You can create a JavaScript function as an expression as shown below:

image

A function expression creates an instance of the function object. It can be passed as an argument to a function, it can be returned from a function, and it can be assigned to a variable or an array. A function expression can be created with the name as shown follows:

Read full article on the Infragistics blog

How do I setup ASP.NET Identity 2.0 to use my own connection string

By default ASP.NET Identity 2.0 creates the database inside the App_Data directory of the application. ASP.NET identity 2.0 works on the Entity Framework Code First approach and it reads the connection string mentioned in the web.config as shown below,

clip_image002

In the ApplicationDbContext class you can find that connection string named DefaultConnection has been used. You can find ApplicationDbContext class inside the IdentityModels.cs file.

clip_image003

You may have a requirement to use some other database server and the database to configure the ASP.NET Identity 2.0. Simplest way you can do this by changing the connection string the web.config file.

clip_image005

DJPC is database server name of my development machine. ASP.NET Identity 2.0 works on the EF Code First approach, it will create the database DelhiDevDayExample, if it’s not already there. Other option is to create connection string programmatically and pass it to the context class.

This is the way you can change the connection string of ASP.NET Identity 2.0.

Hoisting in JavaScript: Simplified

I often see JavaScript developers are struggling to understand HOISTING in JavaScript. In this post let us try to understand the hoisting in simple words.

All JavaScript variables are hoist at the top of the scope (function) in which they are defined. To understand it consider the code snippet shows below,

    var foo = "foo";
    console.log(foo);

Above code will print foo in the console. Simple isn’t it? Now let us go ahead and add a function as shown in below listing,

    var foo = "foo";
    console.log(foo);
    (function () {
        console.log(foo);
    }
    )();

foo will be printed twice on the console. We saw that a variable defined outside a function scope or in global function scope can be used inside other functions. Let us go ahead and modify the function as shown below,

 

    var foo = "foo";
    console.log(foo); // print foo

    (function () {

        console.log(foo); // print undefined
        var foo = "zoo";
    }
    )();

To be surprised now second console statement will print the value undefined. Well, why it is happening? These are the following reasons of that,

  • We have again declared foo inside the anonymous function.
  • As we discussed earlier, JavaScript always hoist a variable declaration at the top of the scope. It only hoist the declaration not the initialization.

In actual, above anonymous function can be seen as below. Declaration has been moved to the top of the function scope, whereas initialization is done at the place variable is declared.

clip_image002

In above function, we are trying to access variable foo before it is initialized and that’s the reason we are getting undefined value printed. There are two types of scoping in the JavaScript,

  • Function scope
  • Global scope

Always a variable is hoisted at either the top of the function scope or global scope. When you use var to declare a variable inside a function, then that variable is inside that particular function scope. When you declare a variable without using var, it is always in the global scope. I hope now you have better understanding of hoisting in JavaScript

You can also read an explanation on LET which is part of ES6 Derick Bailey blog

Getting started with Unit Testing JavaScript using QUnit

It is good to have automated unit tests for the codes while developing. In unit test we test smallest unit of the code for a particular behaviour. The Unit test helps us to find bugs in the code early in the development cycle. Essentially unit test is the piece of code which verifies behaviour of a particular unit of the code in the development phase. Unit tests can be run by the test runner multiple times to verify behaviour of a particular unit of code for different set of input. Now a day’s most of the application development are adhering to Agile and TDD approach. In the test driven development approach, first you write a unit test, it will fail, then write application code to pass the test. Unit testing is not different in JavaScript than other programming languages. To do unit testing or TDD in JavaScript, you need a testing framework. There are many popular JavaScript testing framework available. Some of them are as follows:

  • · Mocha
  • · Jasmine
  • · QUnit
  • · JSUnit

In this article we will focus on QUnit. QUnit is a unit testing framework provided by the jQuery team. It provides a rich set of test assertions, highly informative test suite UI, support of synchronous and asynchronous call back, support of test module etc. In this post we will cover the following topics,

  • Write first unit test
  • Understating the test suite UI
  • A look into the assertions
  • Grouping the tests

Writing the first test

Let us start with setting up the QUnit for the JavaScript unit testing. You need to add reference of the two QUnit files on the HTML page. Either you can have files locally in the project or you can use the reference of the jQuery CDN. I am going to use the CDN option as shows below:

Read full article here on Infragistics blog

Launched Video Blog: Watch to Learn with Dhananjay Kumar

I am excited to announce that, I have started a new video blog

WATCH TO LEARN WITH DHANANJAY KUMAR

clip_image002

On this blog, I will  focus on MEAN stack, C#, Azure, JavaScript etc. To start with I have published 5 episodes. Feel free to provide feedback such that we can together learn better.

I will try to keep Watch to Learn free. However you can support me with the donations. Feel free to subscribe and share.

So let us WATCH TO LEARN WITH DHANANJAY KUMAR  :) :)