Few important concepts of JavaScript

Sourav.Kayal
Posted by in JavaScript category on for Beginner level | Points: 250 | Views : 1435 red flag

In this article we will learn few important concepts of JavaScript, it will be helpful in application development in JavaScript.

Few important utilities in JavaScript

In this article we will learn few important concepts of JavaScript which might helpful in project development. Let’s learn them one by one.

print() function to pint page

Printing operation in web application is very common. We have seen that there is print option in web page and we can get printout of that page.

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
</head>
<body>
    <script>
        function printPage() {
            window.print();
        }
 
    </script>
    <input type="button" name="Print" value="Print" onclick="printPage()" />
</body>
</html>

Here is sample example.


escape character in JavaScript

We cannot put double quote (“”) in string. In below article we have place double quote within string and it will show error.

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
</head>
<body>
    <script>
            document.write("It is an "escape" character");
    </script>
</body>
</html>


To solve this problem we have to use escape ‘\’ before escape character like below.

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
</head>
<body>
    <script>
            document.write("They call it an \"escape\" character");
    </script>
</body>
</html>

Here is sample output of above code.


Open prompt in JavaScript

Opening prompt in JavaScript is common operation. We can open prompt by calling prompt() function of window object.

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
</head>
<body>
    <script>
        function showMessage() {
            var value = window.prompt('Enter Name');
            console.log(value);
        }
    </script>
    <input type="button" name="prompt" value="Show" onclick="showMessage()" />
</body>
</html>

Here is sample example of above output.



 Check function before call

It’s good practice to check function before it call. We can check whether this function is present or not by checking typeof operator.

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
</head>
<body>
    <script>
        function hello() {
            console.log('I am function');
        }
        if (typeof hello === "function")
            hello();
 
    </script>
</body>
</html>


Implement nested function.

We can implement nested function in JavaScript. In this example we have implemented nested function in JavaScript.

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
</head>
<body>
    <script>
        function hypotenuse(a, b) {
            function square(x) {
                return x * x;
            }
            return Math.sqrt(square(a) + square(b));
        }
        console.log(hypotenuse(1, 2));
 
    </script>
</body>
</html>


Conclusion:-

In this article we have learned few important concepts of JavaScript. Hope you have understood them and going to implement in your next JavaScript application.

Page copy protected against web site content infringement by Copyscape

About the Author

Sourav.Kayal
Full Name: Sourav Kayal
Member Level: Silver
Member Status: Member,MVP
Member Since: 6/20/2013 2:09:01 AM
Country: India
Read my blog here http://ctrlcvprogrammer.blogspot.in/
http://www.dotnetfunda.com
I am .NET developer working for HelixDNA Technologies,Bangalore in healthcare domain. Like to learn new technology and programming language. Currently working in ASP.NET ,C# and other microsoft technologies.

Login to vote for this post.

Comments or Responses

Login to post response

Comment using Facebook(Author doesn't get notification)