Beginner JavaScript Part 1

JavaScript is a programming language that runs in your browser. It is the only client-side (runs in your browser) programming language. Its original purpose was to add a little interactivity to static web pages, but now days, it is a full-fledged programming language.

A static web page is pretty boring. Besides looking at the content, the user cannot interact with it. He can fill out forms, but any form or button that the user clicks causes an entire page refresh.

The user’s experience is vastly improved with JavaScript.

Similar to CSS, Javascript can go in a special “script” HTML tag.

Code Dissection

<script>
    const welcomeMessage = 'Hello, world!';
</script>

<script>…</script>

Here is the unique thing about JavaScript.

All JavaScript goes inside of a special HTML tag, the script tag. The script tag tells the browser to run the code inside of the script element. Most of the time, the JavaScript tag goes at the bottom of the HTML page within the body tag.

<html>
    <body>
        <footer>
        </footer>
        <script>...</script>
    </body>
</html>

Javascript has similarities to many other programming languages.

Code Dissection

const

This special ward tells the JavaScript engine that we’re about to create a variable. We can also use “let” and “var” to create a variable, but avoid “var” because it’s being phased out.

welcomeMessage

The text that comes after the “variable instantiater word (const/let)” is the name of the variable. Variables do not start with any special characters. Most of the time, variable names are camel-cased. All the words except the first are capitalized. What you call your variable names is very important. Giving your variables descriptive names makes your code much easier to read. Descriptive variable names are a great way to make your code self-documenting, which means you don’t have to explain what it’s doing.

=

The equals sign is used to assign data to a variable. If you use the “const” word, the data of the variable cannot be changed.

‘Hello, world!’

This sentence is the data that we’re attaching to the variable name.

In JavaScript, there are a couple types of data. Any text surrounded with single or double quotes is a string. We’ll go over the other data types as we run into them.

;

The semicolon marks the end of a statement, but in JavaScript, the semicolon is optional. It’s considered good manners to put it anyway.