In some situations, we will want to be more specific and perhaps define a range of acceptable values that will drive our result. To see the results: open index.html in your browser and open the console, Press Ctrl+Shift+J (Windows / Linux) or Cmd+Opt+J (Mac). We then write the code we want our conditional statement to return if true. getDay only provides values from 0-6 and 5 & 6 both represent Saturday and Sunday, it is safe to assume any values falling in this range will be the weekend. In our code example we use a conditional statement to determine if the day is greater than 4. getDay to retrieve the day of the week (which is a number 0 – 6 starting at Sunday). We will look more at functions in a future lesson, but for now all you need to understand is that new Date() sets our currentDate variable to the current date and time. See JavaScript Date Methods for more information on this function. In this example we are using the built in JavaScript date function in order to retrieve the current day. Alternatively, if you followed along in the previous lesson, use the existing script.js file and replace its content with code below. → Try it outĬreate two files called index.html and script.js and enter the code below into each. In JavaScript, a block of code is surrounded by curly braces. Up until now, we have been examining single line statements. In JavaScript, the criteria or condition is surrounded in parenthesis and the resulting code to run is contained in a block. JavaScript Manipulating the Document Object Model (DOM)Ĭonditional statements (commonly called if statements) provide a way for JavaScript to make decisions and run specified code based on a set of criteria.JavaScript Arrays – Properties and Methods.
0 Comments
Leave a Reply. |