JavaScript Intro

Cmd + Option + C launches Safari console

<script src="script.js"></script> links JavaScript file to a webpage

alert("hello there");
document.write("Now it works!");
console.log("program complete");

sample commands

<script>
  alert("hello there");
  console.log("Begin program");
  document.write("Welcome to JavaScript Basics");
  console.log("End program");
</script>

inserts JavaScript directly into webpage

variables

var myName = "David";
alert(myName);
myName = "Not David anymore";
alert(myName);

‘var’ is only used to create the variable name, not reassign its value

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Lexical_grammar#Keywords -current and future reserved variable names -variable name cannot start with a number -can only contain letters, numbers, $ and _ -can capitalize every letter after the first

two common value types: strings & numbers

strings can use ‘’ or “”, just have to remain consistent var htmlSnippet = '<h1 class = "special">Important Headline</h1>';

var message = 'She\'s a great person!'; backslash character serves as an escape character, quote mark is ignored

prompt('What is your name?');
var visitorName = prompt('What is your name?');
alert(visitorName);
console.log(visitorName);
var visitor = prompt('What is your name?');
var message = 'Hello' + " " + visitor;

concatenation, combining strings or variables document.write(message);

var message = "Hello" + visitor + " .Welcome to Treehouse. ";
message = message + "We are so glad that you came by to visit, ";
message += "please come again, when you want to learn some more.";

same updates the variable

working with strings & variables

var passphrase = "Open Sesame"; console.log(passphrase.length);

  • objects can be a string, the document, the browsers console, variable
  • objects have properties such as length of a string
  • objects have mehtods, which are actions the object can
  • perform. .write or .toUpperCase()
    var stringToShout = prompt('What should I shout?');
    var shout = stringToShout.toUpperCase();
    shout += '!!!';
    alert(shout);
    
    var adjective = prompt('Please type an adjective');
    var verb = prompt('Please type a verb');
    var noun = prompt('Please type a noun');
    alert('All done. Ready for the message?');
    var story = '<h2>There once was a ' + adjective;
    story += ' programmer who wanted to use JavaScript to ';
    story += verb + ' the ' + noun + '.</h2>';
    document.write(story);
    

    adlib story program

numbers

Integers | Floating Point Numbers | Scientific Notation :————–:|:———————————:|:————————-: 5 | 3.14 | 9e-6 0 | -9.8888 | 9e+6 -100 | 0.0009 | 9999 | |

parseInt() convert string to whole number parseFloat() convert string to float, decimal number

var HTMLBadges = prompt("How many HTML badges do you have?");
var CSSBadges = prompt("How many CSS badges do you have?");
var totalBadges = parseInt(HTMLBadges) + parseInt(CSSBadges);
alert("Wow! You have " + totalBadges + " badges!");
var questions = 3;
var questionsLeft = '['+ questions + ' questions left]';
var adjective = prompt('Please type an adjective' + questionsLeft);
questions -= 1;
questionsLeft = '[' + questions + ' questions left]';
var verb = prompt('Please type a verb' + questionsLeft);
questions -= 1;
questionsLeft = '[' + questions + ' questions left]';
var noun = prompt('Please type a noun' + questionsLeft);
alert('All done. Ready for the message?');
var sentence = "<h2>There once was a " + adjective;
sentence += ' programmer who wanted to use JavaScript to ' + verb;
sentence += ' the ' + noun + '.</h2>';
document.write(sentence);

Math.round() function returns the value of a number rounded to the nearest integer

// Returns the value -21
x = Math.round(-20.51);

Math.random() function return a floating number in the range [0, 1] Returns number (1-6)

Math.floor(Math.random() * 6) + 1

var dieRoll = Math.floor(Math.round() * 6) + 1;
alert('You rolled a ' + dieRoll);

```js var input1 = prompt(“Please type a starting number”); var bottomNumber = parseInt(input1); var input = prompt(“Please type a ending number”); var topNumber = parseInt(input); var randomNumber = Math.floor(Math.random() * (topNumber - bottomNumber + 1)) + bottomNumber; var message = randomNumber + “ is a number between “ + bottomNumber + “ and “ + topNumber; document.write(message);