Learn more about JavaScript Operators

Learn more about JavaScript Operators : Operators are another key part of JavaScript. They are a way of changing the value of a variable. Different kinds of operators do different things to the information in your variable.

Assignment operators allow you to create and set the values of your variables.

Equals(=): One uses the equals sign (=) to give your variable a value. You can use it to set variables to both numbers and words. If your variable is a number, it doesn’t have to be in double quotes (” “).

Here’s an example where the value is set to a number:

var userMembers = 3;

Remember the = sign is the assign operator.

And here’s an example where the value is set to words and needs double quotes (” “):

var memberLeader = "Bob";

Arithmetic operators

Arithmetic operators are a way of using math calculations to change the values of your variables.

They are a useful way of creating a numerical value for a variable.

Addition (+): One uses the addition operator (+) to add together numbers to create a value.

Here the value of our userCount variable will be set to 5.

var userCount = 2 + 1;

Subtraction (−): you use the subtraction operator () to subtract numbers to create a value.

Here the value of our soapSupply variable will be set to 1.

var soapSupply = 5 − 4;

Always make sure you use a semi-colon (;) at the end of the variable.

Don’t forget to put text values in double quotes “”.

USING VARIABLES AND OPERATORS

Variables and operators are important parts of the JavaScript programming language. Let’s code some simple JavaScript programs using them.

1) Before we can write any JavaScript we need to create an HTML file with a < script > tag in the < body >.

Open up your text-editing program.

Create a new HTML file called variables.html.

Type this code into your new file:

<!DOCTYPE html>
<html>
<head>
 <title>Variables</title>
</head>
<body>
 <script>
 </script>
</body>
</html>

2) Now try writing some JavaScript. Create a variable between the opening and closing < script > tags.

Give your variable a name and then use the assignment operator (=) to set the value of your variable.

Let’s try with a number, like this:

<script>
 var howmanyShoes = 300;
</script>

Don’t forget JavaScript is case sensitive, so make sure you keep the cases the same in your code block. Also, remember the semi-colon (;) at the end.

If you save your HTML file and open it in your browser, nothing will happen – but don’t worry.

You have stored 300 in your howmanyShoes variable.

3) Let’s use the alert function to check that your variable has been stored in your browser.

Underneath your variable type this code:

<script>
 var howmanyShoes = 300;
 alert(howmanyShoes);
</script>

What we’re doing is asking our browser to run the built-in alert function.

Whatever value is in our howmanyShoes variable will be displayed in an alert.

Check that pop-ups haven’t been disabled in your browser. Search online for how to disable the pop-up blocker in the browser you are using.

Curious to see the code of our Click Me to run code above. button? Some users asked for the code to add buttons on your own websites: (Just cut and paste into HTML file and adjust accordingly.)

<button onclick="myFunction()">Click Me to run code above.</button>

<script>
function myFunction() {
 var howmanyShoes = 300;
 alert(howmanyShoes);
}
</script>

The HTML < button > tag is used to create a button. The < script > tag is then used to carry a function that gets executed every time the button is clicked. Very simple button implementation with JavaScript.

 

Related Links:

Online JavaScript Compiler. Code on the go.

Connected through code, Choose Your Platform!

About the Author: Bernard Aybout

In the land of bytes and bits, a father of three sits, With a heart for tech and coding kits, in IT he never quits. At Magna's door, he took his stance, in Canada's wide expanse, At Karmax Heavy Stamping - Cosma's dance, he gave his career a chance. With a passion deep for teaching code, to the young minds he showed, The path where digital seeds are sowed, in critical thinking mode. But alas, not all was bright and fair, at Magna's lair, oh despair, Harassment, intimidation, a chilling air, made the workplace hard to bear. Management's maze and morale's dip, made our hero's spirit flip, In a demoralizing grip, his well-being began to slip. So he bid adieu to Magna's scene, from the division not so serene, Yet in tech, his interest keen, continues to inspire and convene.