Basics of Programming

CS133JS Beginning Programming: JavaScript

Topics by Week 
1. Intro to JavaScript programming6. Arrays
2. Functions, Variable scope, Operators and Expressions7. Objects and Object Constructors
3. Conditional Statements8. Objects
4. Loops9. DOM
5. Midterm10. Final

 

Table of Contents

Announcements for Fall 2024

Intro to Programming

Programs are sets of instructions. They implement Algorithms.

An algorithm is a step-by-step set of instructions for doing something.

Example: Imagine you had to tell a robot how to make a grilled cheese sandwich. You would need to give it very specific instructions like this:

  1. Collect the following items before cooking:

    • 2 slices of bread

    • 2 teaspoons of butter

    • 2 slices of cheese

    • 1 teaspoon of whole grain mustard

  2. Spread one side of each slice of bread with butter.

  3. Spread a thin layer of mustard on one slice of bread.

  4. Place both slices of cheese on top of the mustard.

  5. Heat a frying pan over medium heat.

  6. Place the sandwich in the pan and cook until both sides are golden brown and cheese is melted.

Exercise: How would we write the algorithm for the hello world program we ran yesterday? (It's listed a few paragraphs down on this page.)

Three control structures

Question: Which control structure was used in the grilled cheese algorithm? What about the hello world program?

Syntax

The grammar of a programming language

JavaScript Programming

JavaScript Variables and Data Types

Variables

Variables are named memory locations.

Example: Hello world

Last time, we wrote this code and ran it in the browser console:

Using Visual Studio Code

This time, we'll put it in a web page, but, first, install Visual Studio Code. And, optionally, add the following extensions:

Putting JS Code in an HTML <script> Element

Using VS Code, start with a standard empty HTML page and add this code to the head element inside of a HTML <script> element:

We can also put script elements in the body of a web page:

Variables that are declared in one <script> element are visible in all the <script> elements that are below it on the page.

 

  1. Let's refactor it to add a second variable and prompt:

Discussion

Data Types

Primitive Types

Variables in JavaScript are dynamically typed, meaning we don't need to declare the type when we declare the variable. JavaScript figures out the type based on the data we assign to it.

Example:

Null and Undefined

This is a bit tricky. If we use the typeof keyword to check the type of a variable and we haven't assigned a value to that variable, then it will show its type as undefined. Also, if we check its value, it will be undefined. So undefined can be either a description of the value or of the data type.

But, null is just a value1. It's the value that means "nothing", which is not the same as zero.

Try this in the console:

 

Mixing data types

What is happening here?

Note: these examples assume the variable declarations at the beginning of this section.

Converting Data Types

Variable Naming Rules and Conventions

 


Creative Commons License Beginning JavaScript Lecture Notes by Brian Bird, written in 2018, revised in , are licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.



1 I disagree with the textbook, Eloquaint JavaScript, on this point. In chapter 1, the author asserts that both null and undefined are types, but I would say that undefined is a type and null is a value.