"Cyber Space" consists of digital information, physical machines and programs ... and people, depending on how you want to look at it. Of the non-human elements, programs are the most important to understand in order to get a handle on cyber security/warfare. This doesn't mean you need to be a programmer (though to be a true technical practitioner of cyber security you do), but you need some understanding of what programs are and the basics of how they work. In this lesson, that's what we'll start to do.
Javascript is a programming language that's primarily used in web pages. This aspect of the language is something we'll look more closely at later. You'll hear Javascript referred to as a "scripting language" and its programs called "scripts". A Program (capital "P") is said to "run on the machine". Javascript programs are not meant to run on a physical machine, but instead to "run on the browser", which itself is a Program running on the physical machine. This kind of "program running within a program", which is actually extremely common, is often referred to as a script.
Numbers, Expressions, Variables
If you enter an expression in the input box to
the right, the interpreter will evaluate the expression
and write its value in the box below.
|
|
Number. Another value type
in Javascript is String. Strings are used to
represent sequences of characters. A string literal,
analogous to a number literal like 76, is anything starting or
ending with the " "s or ' 's. So, "hello world!"
and 'hello world!' are both string literals.
I cannot overestimate the importance in this course, in cyber
security and in life of strings! Here are some things to know
about strings in Javascript.
var s = "the"; s;can't is
easy with " "s, but not with ' 's. Try
entering 'can't'
in the interpreter and see what the problem is. Conversely,
a string like the "it" is not important is easy
to write with ' 's but not with " "s. What about the string:
I didn't say "hello" to you!How could I set a variable equal to such a string? Inside a " "-delimited string, the " character has a special meaning, putting a \ character in front of it "escapes" that special meaning. Same thing works for a ' '-delimited string. Thus either of these two statements
var s1 = "I didn't say \"hello\" to you!"; var s2 = 'I didn\'t say "hello" to you!';are OK. Of course this begs the question" "How do I write a \ character?" The answer is, escape it!
var s3 = "You can put \\ in a string!";I recommend putting all of those in the interpreter and seeing what values the variables get.
"man" + "age" evaluates to "manage".
The + operator works this way regardless of whether we have
literal strings or variables that refer to strings. So, for
example, if var x = "man" and var y =
"age", then x + y evaluates to "manage".
Don't forget: with strings, + means concatenation, not addition.
[index].
So, if var x = "hard", the expression x[0]
evaluates to h, the expressions x[1]
evaluates to a, etc.
"" or ''.
.length at the end of a string to
create an expression that evaluates to a string's length.
So, for example, "hard".length evaluates to 4.
.charCodeAt(index)
Example: "hard".charCodeAt(0) yields 104,
because 104 is the ASCII value of the character h.
String.fromCharCode(number)
Example: String.fromCharCode(104); yields "h"
typeof, and type conversionsNumber and String. There are other
types (Boolean, Function and Object),
though they'll be less relevant to us given the small amount we
do with Javascript.
Javascript has a special operator, typeof, that can
be used to determine the type of a value. For example,
typeof(7) → Number, and
typeof("foo") → String.
We can call typeof for any expression. You might
ask what kind of value typeof returns. Here's how
to find out: enter typeof(typeof(4)) into the
interpreter. So now you know, typeof returns
a String that gives the name of the type.
Here's some interesting ones to try:
typeof(7+5);
typeof("7"+"5");
typeof(7*"5");
typeof(7+"5");
What's going on here? Javascript automatically converts numbers
to strings and strings to numbers when it deems it to be
necessary. If an arithmetic expression involves strings and any operation other than +,
Javascript will convert the string to a number. Because the + operation
means something special to strings, Javascript won't convert
strings to numbers in that case. In fact, if you mix strings
and numbers in a + expression, the numbers get converted to strings,
which the last of the above examples demonstrates.
This kind of type conversion that happens automatically behind the scenes is
called an implicit conversion. There
are explicit conversions too. If you wrap a string in
Number( ) the interpreter attempts to convert the
string to a number. For example, try
3 + Number("7") and compare what you get to
3 + "7". Similarly, if you wrap a number in
String( ) the interpreter attempts to convert the
number to string. For example, try
typeof(String(365)).
To say a word about the three other types:
Math.sqrt( ) are examples
of functions. Programmers can define their own
functions as well as use the extensive library of built-in
functions like the Math functions we've already seen.
1974: DoD is using 450 different programming languages, spending $3 billion/year on software maintenance ($14 billion in 2012 dollars). A major part of "software maintenance" is fixing BUGS!. A search begins for an existing language most suited to DoD requirements ...
1977: No existing programming language is suitable. DoD solicits proposals for a language appropriate for embedded computer applications (i.e., command and control, communications, avionics, shipboard, test equipment, software development and maintenance, and support applications). ["Steelman"]
1980: DoD completes specification of the "ADA" language and subsequently mandates its use.
Here's ADA code essentially equivalent to prompt( "Hello, World!" );
with Text_IO; use Text_IO;
procedure Hello is
begin
Put_Line("Hello, World!");
end Hello;
1996: DoD now using only 37 different programming languages. Much new code is being written in ADA, which is easier (thus less costly) to maintain: ADA was designed to be less prone to the kind of bugs that make software vulnerable to attack.
Today: ADA code is executing on US Navy systems such as the Aegis Weapon System, SSN-21 AN/BSY-2 Submarine Combat Control System, V-22 Osprey, AGM-114 Hellfire missile, CH46 Cockpit Control System, Tomahawk missiles, MK 41 Vertical Launch System.
alert( ) and prompt( )alert( ) pops up a window with a message given by
whatever string you put between the ( )s. Try entering
alert("Hello World!"); in our Javascript
interpreter. Because this doesn't rely on having some kind of
interpreter window, alert( ) is a good way to
communicate information to the user in a program. To get values
from the user, Javascript has a function called prompt( ).
You give it a message indicating to the user what information is
supposed to be provided, and prompt( ) pops up a
window with the message and an input line. Whatever string gets entered
on that line gets returned as the value of the prompt( )
function.
//
until the next newline character is ignored by the interpreter,
and so we use // to write comments.
Below is a simple program that gets the users height and weight and returns to the user what his weight would be if he were 25 feet tall. Play with it, understand it, and see if you can modify it so that it asks the user for a target height (in feet) rather than always using 25.
total keeps a running total of the cost, which
gets updated as the user orders a certain number of burgers, then a
certain number of fries, then a certain number of drinks.
An interesting twist is to ask for the program to repeat the user's
order before giving the total. We can accomplish this by keeping a
running list of what gets ordered. The variable total is a
number, and is initally zero to indicate that, at the beginning, the
user hasn't yet ordered anything. To keep a running list of what's
been ordered, we need a variable (we'll call it order) of
type string. Initially, since nothing has been ordered,
that string will be the empty string, i.e. "".
In this second version of the program, all three variables
— total, num and order
— are constantly being assigned new values.
