[fusion_builder_container type=”flex” hundred_percent=”no” equal_height_columns=”no” menu_anchor=”” hide_on_mobile=”small-visibility,medium-visibility,large-visibility” class=”” id=”” background_color=”” background_image=”” background_position=”center center” background_repeat=”no-repeat” fade=”no” background_parallax=”none” parallax_speed=”0.3″ video_mp4=”” video_webm=”” video_ogv=”” video_url=”” video_aspect_ratio=”16:9″ video_loop=”yes” video_mute=”yes” overlay_color=”” video_preview_image=”” border_color=”” border_style=”solid” padding_top=”” padding_bottom=”” padding_left=”” padding_right=””][fusion_builder_row][fusion_builder_column type=”1_1″ layout=”1_1″ background_position=”left top” background_color=”” border_color=”” border_style=”solid” border_position=”all” spacing=”yes” background_image=”” background_repeat=”no-repeat” padding_top=”” padding_right=”” padding_bottom=”” padding_left=”” margin_top=”0px” margin_bottom=”0px” class=”” id=”” animation_type=”” animation_speed=”0.3″ animation_direction=”left” hide_on_mobile=”small-visibility,medium-visibility,large-visibility” center_content=”no” last=”true” min_height=”” hover_type=”none” link=”” border_sizes_top=”” border_sizes_bottom=”” border_sizes_left=”” border_sizes_right=”” first=”true”][fusion_text columns=”” column_min_width=”” column_spacing=”” rule_style=”” rule_size=”” rule_color=”” hue=”” saturation=”” lightness=”” alpha=”” user_select=”” awb-switch-editor-focus=”” content_alignment_medium=”” content_alignment_small=”” content_alignment=”” hide_on_mobile=”small-visibility,medium-visibility,large-visibility” sticky_display=”normal,sticky” class=”” id=”” width_medium=”” width_small=”” width=”” min_width_medium=”” min_width_small=”” min_width=”” max_width_medium=”” max_width_small=”” max_width=”” margin_top=”” margin_right=”” margin_bottom=”” margin_left=”” fusion_font_family_text_font=”” fusion_font_variant_text_font=”” font_size=”” line_height=”” letter_spacing=”” text_transform=”” text_color=”” animation_type=”” animation_direction=”left” animation_color=”” animation_speed=”0.3″ animation_delay=”0″ animation_offset=”” logics=””]
🧠 Learning Objectives
By the end of this lesson, learners will be able to:
-
Understand what variables and operators are in JavaScript.
-
Use assignment and arithmetic operators.
-
Write simple JavaScript inside an HTML file.
-
Display variable values using the alert() function.
📋 Section 1: What are Variables?
Variables are containers used to store data such as numbers or strings (text).
Example
[/fusion_text][fusion_syntax_highlighter theme=”” language=”x-sh” line_numbers=”” line_wrapping=”” copy_to_clipboard=”” copy_to_clipboard_text=”” hide_on_mobile=”small-visibility,medium-visibility,large-visibility” class=”” id=”” font_size=”” border_size=”” border_color=”” hue=”” saturation=”” lightness=”” alpha=”” border_style=”” background_color=”” line_number_background_color=”” line_number_text_color=”” margin_top=”” margin_right=”” margin_bottom=”” margin_left=””]dmFyIG15QWdlID0gMjU7CnZhciBteU5hbWUgPSAiQWxpY2UiOwo=[/fusion_syntax_highlighter][fusion_text columns=”” column_min_width=”” column_spacing=”” rule_style=”” rule_size=”” rule_color=”” hue=”” saturation=”” lightness=”” alpha=”” user_select=”” awb-switch-editor-focus=”” content_alignment_medium=”” content_alignment_small=”” content_alignment=”” hide_on_mobile=”small-visibility,medium-visibility,large-visibility” sticky_display=”normal,sticky” class=”” id=”” width_medium=”” width_small=”” width=”” min_width_medium=”” min_width_small=”” min_width=”” max_width_medium=”” max_width_small=”” max_width=”” margin_top=”” margin_right=”” margin_bottom=”” margin_left=”” fusion_font_family_text_font=”” fusion_font_variant_text_font=”” font_size=”” line_height=”” letter_spacing=”” text_transform=”” text_color=”” animation_type=”” animation_direction=”left” animation_color=”” animation_speed=”0.3″ animation_delay=”0″ animation_offset=”” logics=””]
In the example:
🧮 Section 2: Assignment Operator (=)
The equals sign (=) assigns a value to a variable.
Example 1 (Number):
[/fusion_text][fusion_syntax_highlighter theme=”” language=”x-sh” line_numbers=”” line_wrapping=”” copy_to_clipboard=”” copy_to_clipboard_text=”” hide_on_mobile=”small-visibility,medium-visibility,large-visibility” class=”” id=”” font_size=”” border_size=”” border_color=”” hue=”” saturation=”” lightness=”” alpha=”” border_style=”” background_color=”” line_number_background_color=”” line_number_text_color=”” margin_top=”” margin_right=”” margin_bottom=”” margin_left=””]dmFyIHVzZXJNZW1iZXJzID0gMzsK[/fusion_syntax_highlighter][fusion_text columns=”” column_min_width=”” column_spacing=”” rule_style=”” rule_size=”” rule_color=”” hue=”” saturation=”” lightness=”” alpha=”” user_select=”” awb-switch-editor-focus=”” content_alignment_medium=”” content_alignment_small=”” content_alignment=”” hide_on_mobile=”small-visibility,medium-visibility,large-visibility” sticky_display=”normal,sticky” class=”” id=”” width_medium=”” width_small=”” width=”” min_width_medium=”” min_width_small=”” min_width=”” max_width_medium=”” max_width_small=”” max_width=”” margin_top=”” margin_right=”” margin_bottom=”” margin_left=”” fusion_font_family_text_font=”” fusion_font_variant_text_font=”” font_size=”” line_height=”” letter_spacing=”” text_transform=”” text_color=”” animation_type=”” animation_direction=”left” animation_color=”” animation_speed=”0.3″ animation_delay=”0″ animation_offset=”” logics=””]
Example 2 (Text/String):
[/fusion_text][fusion_syntax_highlighter theme=”” language=”x-sh” line_numbers=”” line_wrapping=”” copy_to_clipboard=”” copy_to_clipboard_text=”” hide_on_mobile=”small-visibility,medium-visibility,large-visibility” class=”” id=”” font_size=”” border_size=”” border_color=”” hue=”” saturation=”” lightness=”” alpha=”” border_style=”” background_color=”” line_number_background_color=”” line_number_text_color=”” margin_top=”” margin_right=”” margin_bottom=”” margin_left=””]dmFyIG1lbWJlckxlYWRlciA9ICJCb2IiOwo=[/fusion_syntax_highlighter][fusion_text columns=”” column_min_width=”” column_spacing=”” rule_style=”” rule_size=”” rule_color=”” hue=”” saturation=”” lightness=”” alpha=”” user_select=”” awb-switch-editor-focus=”” content_alignment_medium=”” content_alignment_small=”” content_alignment=”” hide_on_mobile=”small-visibility,medium-visibility,large-visibility” sticky_display=”normal,sticky” class=”” id=”” width_medium=”” width_small=”” width=”” min_width_medium=”” min_width_small=”” min_width=”” max_width_medium=”” max_width_small=”” max_width=”” margin_top=”” margin_right=”” margin_bottom=”” margin_left=”” fusion_font_family_text_font=”” fusion_font_variant_text_font=”” font_size=”” line_height=”” letter_spacing=”” text_transform=”” text_color=”” animation_type=”” animation_direction=”left” animation_color=”” animation_speed=”0.3″ animation_delay=”0″ animation_offset=”” logics=””]
➕ Section 3: Arithmetic Operators
JavaScript allows you to perform basic math using arithmetic operators.
| Operator |
Description |
Example |
Result |
+ |
Addition |
var x = 2 + 3; |
5 |
- |
Subtraction |
var y = 7 - 2; |
5 |
Example
[/fusion_text][fusion_syntax_highlighter theme=”” language=”x-sh” line_numbers=”” line_wrapping=”” copy_to_clipboard=”” copy_to_clipboard_text=”” hide_on_mobile=”small-visibility,medium-visibility,large-visibility” class=”” id=”” font_size=”” border_size=”” border_color=”” hue=”” saturation=”” lightness=”” alpha=”” border_style=”” background_color=”” line_number_background_color=”” line_number_text_color=”” margin_top=”” margin_right=”” margin_bottom=”” margin_left=””]dmFyIHVzZXJDb3VudCA9IDIgKyAzOyAgIC8vIHVzZXJDb3VudCBpcyA1CnZhciBzb2FwU3VwcGx5ID0gNSAtIDQ7ICAvLyBzb2FwU3VwcGx5IGlzIDEK[/fusion_syntax_highlighter][fusion_text columns=”” column_min_width=”” column_spacing=”” rule_style=”” rule_size=”” rule_color=”” hue=”” saturation=”” lightness=”” alpha=”” user_select=”” awb-switch-editor-focus=”” content_alignment_medium=”” content_alignment_small=”” content_alignment=”” hide_on_mobile=”small-visibility,medium-visibility,large-visibility” sticky_display=”normal,sticky” class=”” id=”” width_medium=”” width_small=”” width=”” min_width_medium=”” min_width_small=”” min_width=”” max_width_medium=”” max_width_small=”” max_width=”” margin_top=”” margin_right=”” margin_bottom=”” margin_left=”” fusion_font_family_text_font=”” fusion_font_variant_text_font=”” font_size=”” line_height=”” letter_spacing=”” text_transform=”” text_color=”” animation_type=”” animation_direction=”left” animation_color=”” animation_speed=”0.3″ animation_delay=”0″ animation_offset=”” logics=””]
💻 Section 4: HTML + JavaScript Example
Let’s create a simple HTML page that runs JavaScript using <script> tags.
Step-by-Step
-
Open any code editor (e.g., VS Code, Notepad++).
-
Create a file called variables.html.
Code Example
[/fusion_text][fusion_syntax_highlighter theme=”” language=”x-sh” line_numbers=”” line_wrapping=”” copy_to_clipboard=”” copy_to_clipboard_text=”” hide_on_mobile=”small-visibility,medium-visibility,large-visibility” class=”” id=”” font_size=”” border_size=”” border_color=”” hue=”” saturation=”” lightness=”” alpha=”” border_style=”” background_color=”” line_number_background_color=”” line_number_text_color=”” margin_top=”” margin_right=”” margin_bottom=”” margin_left=””]PCFET0NUWVBFIGh0bWw+CjxodG1sPgo8aGVhZD4KICA8dGl0bGU+SmF2YVNjcmlwdCBWYXJpYWJsZXM8L3RpdGxlPgo8L2hlYWQ+Cjxib2R5PgoKPHNjcmlwdD4KICB2YXIgaG93bWFueVNob2VzID0gMzAwOwogIGFsZXJ0KGhvd21hbnlTaG9lcyk7Cjwvc2NyaXB0PgoKPC9ib2R5Pgo8L2h0bWw+Cg==[/fusion_syntax_highlighter][fusion_text columns=”” column_min_width=”” column_spacing=”” rule_style=”” rule_size=”” rule_color=”” hue=”” saturation=”” lightness=”” alpha=”” user_select=”” awb-switch-editor-focus=”” content_alignment_medium=”” content_alignment_small=”” content_alignment=”” hide_on_mobile=”small-visibility,medium-visibility,large-visibility” sticky_display=”normal,sticky” class=”” id=”” width_medium=”” width_small=”” width=”” min_width_medium=”” min_width_small=”” min_width=”” max_width_medium=”” max_width_small=”” max_width=”” margin_top=”” margin_right=”” margin_bottom=”” margin_left=”” fusion_font_family_text_font=”” fusion_font_variant_text_font=”” font_size=”” line_height=”” letter_spacing=”” text_transform=”” text_color=”” animation_type=”” animation_direction=”left” animation_color=”” animation_speed=”0.3″ animation_delay=”0″ animation_offset=”” logics=””]
When you open this file in a browser, it will display a popup with the value 300.
🔘 Section 5: Adding a Button to Run JavaScript
Create a button that triggers a script:
[/fusion_text][fusion_syntax_highlighter theme=”” language=”x-sh” line_numbers=”” line_wrapping=”” copy_to_clipboard=”” copy_to_clipboard_text=”” hide_on_mobile=”small-visibility,medium-visibility,large-visibility” class=”” id=”” font_size=”” border_size=”” border_color=”” hue=”” saturation=”” lightness=”” alpha=”” border_style=”” background_color=”” line_number_background_color=”” line_number_text_color=”” margin_top=”” margin_right=”” margin_bottom=”” margin_left=””]PCFET0NUWVBFIGh0bWw+CjxodG1sPgo8aGVhZD4KICA8dGl0bGU+Q2xpY2sgQnV0dG9uPC90aXRsZT4KPC9oZWFkPgo8Ym9keT4KCjxidXR0b24gb25jbGljaz0ic2hvd01lc3NhZ2UoKSI+Q2xpY2sgTWUgdG8gcnVuIGNvZGUgYWJvdmU8L2J1dHRvbj4KCjxzY3JpcHQ+CmZ1bmN0aW9uIHNob3dNZXNzYWdlKCkgewogIHZhciBtZXNzYWdlID0gIkphdmFTY3JpcHQgaXMgd29ya2luZyEiOwogIGFsZXJ0KG1lc3NhZ2UpOwp9Cjwvc2NyaXB0PgoKPC9ib2R5Pgo8L2h0bWw+Cg==[/fusion_syntax_highlighter][fusion_text columns=”” column_min_width=”” column_spacing=”” rule_style=”” rule_size=”” rule_color=”” hue=”” saturation=”” lightness=”” alpha=”” user_select=”” awb-switch-editor-focus=”” content_alignment_medium=”” content_alignment_small=”” content_alignment=”” hide_on_mobile=”small-visibility,medium-visibility,large-visibility” sticky_display=”normal,sticky” class=”” id=”” width_medium=”” width_small=”” width=”” min_width_medium=”” min_width_small=”” min_width=”” max_width_medium=”” max_width_small=”” max_width=”” margin_top=”” margin_right=”” margin_bottom=”” margin_left=”” fusion_font_family_text_font=”” fusion_font_variant_text_font=”” font_size=”” line_height=”” letter_spacing=”” text_transform=”” text_color=”” animation_type=”” animation_direction=”left” animation_color=”” animation_speed=”0.3″ animation_delay=”0″ animation_offset=”” logics=””]
🧪 Final Assignment
Create your own interactive HTML page that:
-
Defines two numeric variables.
-
Adds them together using the + operator.
-
Shows the result in an alert box.
-
Bonus: Create a button that shows a message when clicked.
📝 Assignment Template
Create a file called assignment.html. Use this outline:
[/fusion_text][fusion_syntax_highlighter theme=”” language=”x-sh” line_numbers=”” line_wrapping=”” copy_to_clipboard=”” copy_to_clipboard_text=”” hide_on_mobile=”small-visibility,medium-visibility,large-visibility” class=”” id=”” font_size=”” border_size=”” border_color=”” hue=”” saturation=”” lightness=”” alpha=”” border_style=”” background_color=”” line_number_background_color=”” line_number_text_color=”” margin_top=”” margin_right=”” margin_bottom=”” margin_left=””]PCFET0NUWVBFIGh0bWw+CjxodG1sPgo8aGVhZD4KICA8dGl0bGU+SmF2YVNjcmlwdCBBc3NpZ25tZW50PC90aXRsZT4KPC9oZWFkPgo8Ym9keT4KCjxzY3JpcHQ+Ci8vIFN0ZXAgMTogRGVjbGFyZSB0d28gbnVtZXJpYyB2YXJpYWJsZXMKLy8gU3RlcCAyOiBBZGQgdGhlbSB1c2luZyArIG9wZXJhdG9yCi8vIFN0ZXAgMzogRGlzcGxheSB0aGUgcmVzdWx0IGluIGFuIGFsZXJ0Cjwvc2NyaXB0PgoKPCEtLSBCb251czogQnV0dG9uIHRvIHNob3cgYSBtZXNzYWdlIC0tPgo8YnV0dG9uIG9uY2xpY2s9ImN1c3RvbU1lc3NhZ2UoKSI+Q2xpY2sgTWU8L2J1dHRvbj4KCjxzY3JpcHQ+Ci8vIEZ1bmN0aW9uIHRvIHNob3cgbWVzc2FnZQo8L3NjcmlwdD4KCjwvYm9keT4KPC9odG1sPgo=[/fusion_syntax_highlighter][fusion_text columns=”” column_min_width=”” column_spacing=”” rule_style=”” rule_size=”” rule_color=”” hue=”” saturation=”” lightness=”” alpha=”” user_select=”” awb-switch-editor-focus=”” content_alignment_medium=”” content_alignment_small=”” content_alignment=”” hide_on_mobile=”small-visibility,medium-visibility,large-visibility” sticky_display=”normal,sticky” class=”” id=”” width_medium=”” width_small=”” width=”” min_width_medium=”” min_width_small=”” min_width=”” max_width_medium=”” max_width_small=”” max_width=”” margin_top=”” margin_right=”” margin_bottom=”” margin_left=”” fusion_font_family_text_font=”” fusion_font_variant_text_font=”” font_size=”” line_height=”” letter_spacing=”” text_transform=”” text_color=”” animation_type=”” animation_direction=”left” animation_color=”” animation_speed=”0.3″ animation_delay=”0″ animation_offset=”” logics=””]
✅ Answer Key
[/fusion_text][fusion_syntax_highlighter theme=”” language=”x-sh” line_numbers=”” line_wrapping=”” copy_to_clipboard=”” copy_to_clipboard_text=”” hide_on_mobile=”small-visibility,medium-visibility,large-visibility” class=”” id=”” font_size=”” border_size=”” border_color=”” hue=”” saturation=”” lightness=”” alpha=”” border_style=”” background_color=”” line_number_background_color=”” line_number_text_color=”” margin_top=”” margin_right=”” margin_bottom=”” margin_left=””]PCFET0NUWVBFIGh0bWw+CjxodG1sPgo8aGVhZD4KICA8dGl0bGU+SmF2YVNjcmlwdCBBc3NpZ25tZW50PC90aXRsZT4KPC9oZWFkPgo8Ym9keT4KCjxzY3JpcHQ+Ci8vIFN0ZXAgMSAmIDIKdmFyIG51bTEgPSAxMDsKdmFyIG51bTIgPSAxNTsKdmFyIHRvdGFsID0gbnVtMSArIG51bTI7CgovLyBTdGVwIDMKYWxlcnQoIlRoZSB0b3RhbCBpczogIiArIHRvdGFsKTsKPC9zY3JpcHQ+Cgo8IS0tIEJvbnVzIC0tPgo8YnV0dG9uIG9uY2xpY2s9ImN1c3RvbU1lc3NhZ2UoKSI+Q2xpY2sgTWU8L2J1dHRvbj4KCjxzY3JpcHQ+CmZ1bmN0aW9uIGN1c3RvbU1lc3NhZ2UoKSB7CiAgdmFyIHVzZXIgPSAiQmVybmFyZCI7CiAgYWxlcnQoIkhlbGxvICIgKyB1c2VyICsgIiwgdGhhbmtzIGZvciBsZWFybmluZyBKYXZhU2NyaXB0ISIpOwp9Cjwvc2NyaXB0PgoKPC9ib2R5Pgo8L2h0bWw+Cg==[/fusion_syntax_highlighter][fusion_text columns=”” column_min_width=”” column_spacing=”” rule_style=”” rule_size=”” rule_color=”” hue=”” saturation=”” lightness=”” alpha=”” user_select=”” awb-switch-editor-focus=”” content_alignment_medium=”” content_alignment_small=”” content_alignment=”” hide_on_mobile=”small-visibility,medium-visibility,large-visibility” sticky_display=”normal,sticky” class=”” id=”” width_medium=”” width_small=”” width=”” min_width_medium=”” min_width_small=”” min_width=”” max_width_medium=”” max_width_small=”” max_width=”” margin_top=”” margin_right=”” margin_bottom=”” margin_left=”” fusion_font_family_text_font=”” fusion_font_variant_text_font=”” font_size=”” line_height=”” letter_spacing=”” text_transform=”” text_color=”” animation_type=”” animation_direction=”left” animation_color=”” animation_speed=”0.3″ animation_delay=”0″ animation_offset=”” logics=””]
Final Summary: Understanding JavaScript Variables and Operators
In this introductory JavaScript lesson, we explored one of the most fundamental aspects of programming—variables and operators. These building blocks are essential to nearly every piece of logic written in JavaScript, forming the foundation for creating dynamic and interactive web applications.
We began by understanding variables, which act as storage containers for different types of data like numbers or words. A variable must be given a unique name and can be assigned values using the assignment operator, represented by the equals sign (=). This operator doesn’t compare values like in math—it assigns the value on the right to the variable on the left.
Next, we introduced arithmetic operators, which are used to perform basic mathematical operations such as addition and subtraction. These tools allow developers to calculate and manipulate numeric values dynamically—whether you’re tracking user input, adjusting a product inventory count, or computing the total cost in a shopping cart.
To demonstrate real-world use, we created a simple HTML page and embedded JavaScript using the <script> tag. We then used a built-in browser function called alert to display the value of a variable, confirming that the code executed successfully. This not only taught us how variables work behind the scenes, but also how to present that data to users through pop-up alerts.
For those ready to go a step further, we added an interactive button that runs a JavaScript function when clicked—showing how HTML and JavaScript work together to bring websites to life.
Whether you’re building a simple calculator, validating user input, or designing a complex application, mastering variables and operators is a critical first step. They empower your scripts to be flexible, responsive, and interactive—key qualities in today’s modern web development landscape.
📎 Resources
[/fusion_text][fusion_text columns=”” column_min_width=”” column_spacing=”” rule_style=”” rule_size=”” rule_color=”” hue=”” saturation=”” lightness=”” alpha=”” user_select=”” awb-switch-editor-focus=”” content_alignment_medium=”” content_alignment_small=”” content_alignment=”” hide_on_mobile=”small-visibility,medium-visibility,large-visibility” sticky_display=”normal,sticky” class=”” id=”” width_medium=”” width_small=”” width=”” min_width_medium=”” min_width_small=”” min_width=”” max_width_medium=”” max_width_small=”” max_width=”” margin_top=”50px” margin_right=”” margin_bottom=”” margin_left=”” fusion_font_family_text_font=”” fusion_font_variant_text_font=”” font_size=”” line_height=”” letter_spacing=”” text_transform=”” text_color=”” animation_type=”” animation_direction=”left” animation_color=”” animation_speed=”0.3″ animation_delay=”0″ animation_offset=”” logics=””]
Related Videos:

Related Posts:
Learn about Python Conditions
Introduction to JavaScript – Review Types and Operators
Introduction to JavaScript – Variables: Mathematical Assignment Operators
Python Basic Operators
Pro PHP Security, 2nd Edition
Introduction to JavaScript – Math Operators
Helpdesk Support Page
Pro PHP Security, 2nd Edition
Introduction to JavaScript – Math Operators
Coding with JavaScript
[/fusion_text][/fusion_builder_column][/fusion_builder_row][/fusion_builder_container]