Skip to main content

Analytics

My Very First JavaScript Code

Digitally Enhanced Shot Of A Handsome Businessman Working In The Office Superimposed Over Multiple Lines Of Computer Code

“I can Talk JavaScript, I can walk JavaScript, I can Laugh JavaScript because JavaScript is a very Funny Language.”

It’s really a fun writing JavaScript code in the beginning like you can show a pop-up alert, can set a condition to show results and you can also do mathematical calculations in the console.
Just press Ctrl+Shift+J and you are there. This shortcut will open the browser console in Windows and Linux where you can write your first JavaScript code as I do. You can also use Option + ⌘ + J on macOS.
Let’s start with our first JavaScript code – An alert pop-up – that may be funny and blowing someone’s mind if you write something like this:

 

alert('Click OK to uninstall the browser')

Javascript Pop Up Alert

 

Now you are thinking that how this can blow someone’s mind if I have to write this in console in front of them.

You can learn in my further blogs that how you can show an alert like this in your webpage by add a simple code in your page source.

 

<button onclick="myFunction()">Try it</button>

<script>
var myVar;

function myFunction() {
    myVar = setTimeout(alertFunc, 3000);
}

function alertFunc() {
  alert("Hello!");
}
</script>

 

Another one is how we can add conditions to show some results. But before this, let us learn about operators that will help you to define the condition i.e. ( = , == , === ).

What is =, == & === in JavaScript?

  1. Equal to “ = ” is an assignment operator which sets the variable on the left of “ = ” to the value of the expression on the right.
    • Example: firstName = “Sanju”;
  2. Double Equal “ == ” is known as the equality operator. It is used to compare two values for equality, without considering their data types. When you use == to compare two values, JavaScript performs type coercion if necessary to make the values comparable.
  3. Triple Equal “ === ” is known as the strict equality operator. It is used to compare two values for both equality of value and equality of type. When you use === to compare two values, JavaScript checks if the values are identical in both type and value.

Here is another basic JavaScript code that will bring you a step ahead in your training.

 

let js = 'Funny'

 

If till now you found the blog funny then you can write js = “funny” else you can use funny only, no other option from my side.

Just kidding!
Assign whatever value you want, it’s on you. Here I assign ‘js’ as ‘funny’ using assignment operator “ = ” and set a string ‘funny’. It will show a result as undefined as we haven’t written here what we want from this value.

Then we will write:

 

if (js === 'Funny') alert(JavaScript is Fun')

 

Here, first we define the js as being funny and here in the next line we said that if js strictly equal (===) to funny then JavaScript should show us an alert window with the text ‘JavaScript is FuN!’. Here we are using strict equality operator so that it will strictly compare both the values as this is very helpful when we are using various variables in our JavaScript File.

Javascript Is Fun

Leave a Reply

Your email address will not be published. Required fields are marked *

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

Sanjeev Kumar

Sanjeev Kumar has worked at Perficient as a Technical Consultant. He is an Adobe Certified Professional - Adobe Analytics Business Practitioner and has 5+ years of experience in Digital Marketing.

More from this Author

Follow Us