Week 5 JavaScript
Week 5: JavaScript Part 1
1. What is JavaScript (JS)¶
JavaScript is a scripting language used to create dynamic content on web pages. It can interact with HTML and CSS to enhance user experiences
JS code is between <script>
and </script>
tags
2. Code editor¶
3. Writing a JavaScript Program¶
- Scripts can be placed in the
<body>
, or in the<head>
section of an HTML page - JS is better to put at the end of
<body>
section, - Saved in external files and access an external script
<script src = "" ></script>
4. JavaScript Syntax¶
- JavaScript command lines end with a semicolon to separate it from the next command line in the program
Exercise 1: Create an empty html and write some simple JS¶
1.1 Write JS within HTML file
- Write JS within HTML file
- Write comments
1.2 Write JS in external file
- Create JS in a folder
- Connect JS in external file to HTML
5. JavaScript Basics¶
5.1 Variables¶
Variables are used to store values: const
, var
, let
,
- The 'var' was used in JS from 1995 to 2015, now should only be used in code for older browsers
- The 'let' and 'const' was added to JS in 2015
- Always use
const
if the value should not be changed
The variables
- can not be a reserved keys
- should be meaningful
- can not start with a number
- can not contain a space a hyphen
- are case sensative
Exercise 2: Create a variable¶
5.2 Constant¶
const
is a variable whose value cannot be changed once it is assignedBelow we tried to change the value of
const
and will cause the error
5.3 Type of Variables¶
- Numeric
- String
- Boolean
- Null
6. Objects in JS¶
Exercise 3: Create a object¶
8.2 Embed function into element¶
- Embedding a function into an HTML element in JavaScript involves setting up event handlers that call a function when the user interacts with the element.
- This allows you to dynamically change or update the content or behavior of the webpage based on user actions.