How to return value from Javascript Function ?


Function is the heart of java script. If you will do anythings in java script then you have to write function.

While working on project so many time we get scenario to retun value from some function and display somewhere.

But here we can do like C# method. We have to include retun at end of function like this

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Function.aspx.cs" Inherits="Javascript_Test_Function" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">

<html xmlns="">
<head runat="server">
    <script type="text/javascript">
        function sayHello() {
            var msg = "I m from Javacript";
            return msg;

        function doSomething() {
            var msg1 = sayHello();
            document.getElementById('<%=txtMsg.ClientID %>').value = msg1;

    <form id="form1" runat="server">
    <asp:Button ID="btnclick" runat="server" OnClientClick="doSomething();return false;" Text="Click Here" />
    <br />
    <br />
    <asp:TextBox ID="txtMsg" runat="server" />


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.