{"id":26871296,"url":"https://github.com/codeadamca/javascript-if-statements","last_synced_at":"2025-10-05T19:59:38.433Z","repository":{"id":46327965,"uuid":"332304712","full_name":"codeadamca/javascript-if-statements","owner":"codeadamca","description":"A basic example of using JavaScript if statements on a webpage. ","archived":false,"fork":false,"pushed_at":"2025-01-26T21:28:56.000Z","size":17,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-10-05T19:59:33.543Z","etag":null,"topics":["if-statements","javascript","learning-code"],"latest_commit_sha":null,"homepage":"","language":"HTML","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/codeadamca.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2021-01-23T20:46:30.000Z","updated_at":"2025-01-26T21:28:59.000Z","dependencies_parsed_at":"2024-09-14T02:14:21.409Z","dependency_job_id":"be2495f3-df45-4851-9b21-127bf223687d","html_url":"https://github.com/codeadamca/javascript-if-statements","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/codeadamca/javascript-if-statements","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/codeadamca%2Fjavascript-if-statements","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/codeadamca%2Fjavascript-if-statements/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/codeadamca%2Fjavascript-if-statements/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/codeadamca%2Fjavascript-if-statements/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/codeadamca","download_url":"https://codeload.github.com/codeadamca/javascript-if-statements/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/codeadamca%2Fjavascript-if-statements/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":278510913,"owners_count":25998997,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-10-05T02:00:06.059Z","response_time":54,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["if-statements","javascript","learning-code"],"created_at":"2025-03-31T07:19:02.578Z","updated_at":"2025-10-05T19:59:38.387Z","avatar_url":"https://github.com/codeadamca.png","language":"HTML","readme":"# A Basic Introduction to JavaScript and If Statements\n\nJavaScript `if` statements allow code to make decisions. A typical `if` statement has the following structure:\n\n```javascript\nif(variable == \"value\")\n{\n  // Code to execute when condition is true\n}\n```\n\nAn `if` statement causes the block of code between the open `{` and close `}` to only be executed if the condition between the open `(` and close `)` is true. When writing a condition, the following operators can be used:\n\n\u003ctable\u003e\n\u003ctr\u003e\u003cth\u003eOperator\u003c/th\u003e\u003cth\u003eDescription\u003c/th\u003e\u003c/tr\u003e\n\u003ctr\u003e\u003ctd\u003e==\u003c/td\u003e\u003ctd\u003eEqual to\u003c/td\u003e\u003c/tr\u003e\n\u003ctr\u003e\u003ctd\u003e!=\u003c/td\u003e\u003ctd\u003eNot equal to\u003c/td\u003e\u003c/tr\u003e\n\u003ctr\u003e\u003ctd\u003e\u0026gt;\u003c/td\u003e\u003ctd\u003eGreater than\u003c/td\u003e\u003c/tr\u003e\n\u003ctr\u003e\u003ctd\u003e\u0026lt;\u003c/td\u003e\u003ctd\u003eLess than\u003c/td\u003e\u003c/tr\u003e\n\u003ctr\u003e\u003ctd\u003e\u0026gt;=\u003c/td\u003e\u003ctd\u003eGreater than or equal to\u003c/td\u003e\u003c/tr\u003e\n\u003ctr\u003e\u003ctd\u003e\u0026lt;=\u003c/td\u003e\u003ctd\u003eLess than or equal to\u003c/td\u003e\u003c/tr\u003e\n\u003c/table\u003e\n\n## Validation\n\nJavaScript `if` statements are commonly used when validating a form. Create a new `HTML` document and add the following code:\n\n```html\n\u003c!doctype html\u003e\n\u003chtml\u003e\n\u003chead\u003e\n    \u003ctitle\u003eJavaScript If Statements\u003c/title\u003e\n\u003c/head\u003e\n\u003cbody\u003e\n\n    \u003ch1\u003eJavaScript If Statements\u003c/h1\u003e\n\n    \u003cp\u003eEnter some text:\u003c/p\u003e\n\n    \u003cinput type=\"text\" id=\"text\"\u003e\n\n    \u003cbr\u003e\n\n    \u003cbutton id=\"check\"\u003eCheck\u003c/button\u003e\n\n\u003c/body\u003e\n\u003c/html\u003e\n```\n\nWhen the button is pressed, we will display a message indicating if the textbox has a value or is blank. We will achieve this by using an `if` statement followed by an `else` statement. The block of code associated to the `else` is executed if the condition of the `if` statement is false:\n\n```javascript\nif(variable == \"value\")\n{\n  // Code to execute when condition is true\n}\nelse\n{\n  // Code to execute when condition is false\n}\n```\n\nAdd a `script` tag below the button in the new doucument. Add the following JavaScript to check the textbox and display a message:\n\n```javascript\n  var button = document.getElementById(\"check\");\n\n  button.addEventListener(\"click\", function(){\n\n      var textbox = document.getElementById(\"text\");\n\n      if(textbox.value == \"\")\n      {\n          alert(\"The textbox is empty!\");\n      }\n      else\n      {\n          alert(\"The textbox has a value\");\n      }\n\n  });\n```\n\n## Try It Out\n\nCreate a new HTML document, add the standard HTML tags, and a title:\n\n```html\n\u003c!doctype html\u003e\n\u003chtml\u003e\n\u003chead\u003e\n    \u003ctitle\u003eJavaScript Greeting\u003c/title\u003e\n\u003c/head\u003e\n\u003cbody\u003e\n\n    \u003ch1\u003eJavaScript Greeting\u003c/h1\u003e\n\n\u003c/body\u003e\n\u003c/html\u003e\n```\n\nAfter the title add a `script` tag and the following JavaScript:\n\n```javascript\nvar now = new Date();\n\nvar day = now.getDay();\nvar hour = now.getHours();\n\ndocument.write(\"\u003cp\u003eThe current day is \" + day + \".\u003c/p\u003e\");\ndocument.write(\"\u003cp\u003eThe current hour is \" + hour + \".\u003c/p\u003e\");\n```\n\nThis code creates a date object and uses the `getDay()` and `getHours()` to extract the day of the week (0 for Sunday and 6 for Saturday) and the hour of the day (in a 24 hours format). \n\nAfter the previous JavaScript add an `if` statement to display a greeting based on the time of day and day of week:\n\n\u003ctable\u003e\n\u003ctr\u003e\u003cth\u003eCondition\u003c/th\u003e\u003cth\u003eMessage\u003c/th\u003e\u003c/tr\u003e\n\u003ctr\u003e\u003ctd\u003eMonday\u003c/td\u003e\u003ctd\u003eDoes someone have a case of the Mondays!\u003c/td\u003e\u003c/tr\u003e\n\u003ctr\u003e\u003ctd\u003eFriday afternoon\u003c/td\u003e\u003ctd\u003eTGIF!\u003c/td\u003e\u003c/tr\u003e\n\u003ctr\u003e\u003ctd\u003eWeekend\u003c/td\u003e\u003ctd\u003eHave a great weekend!\u003c/td\u003e\u003c/tr\u003e\n\u003ctr\u003e\u003ctd\u003eOther mornings\u003c/td\u003e\u003ctd\u003eGood morning!\u003c/td\u003e\u003c/tr\u003e\n\u003ctr\u003e\u003ctd\u003eOther afternoons\u003c/td\u003e\u003ctd\u003eGood afternoon!\u003c/td\u003e\u003c/tr\u003e\n\u003c/table\u003e\n\n\u003e Full tutorial URL:  \n\u003e https://codeadam.ca/learning/javascript-if-statements.html\n\n***\n\n## Repo Resources\n\n* [Visual Studio Code](https://code.visualstudio.com/)\n* [W3Schools - JavaScript](https://www.w3schools.com/js/)\n\n\u003cbr\u003e\n\u003ca href=\"https://codeadam.ca\"\u003e\n\u003cimg src=\"https://cdn.codeadam.ca/images@1.0.0/codeadam-logo-coloured-horizontal.png\" width=\"200\"\u003e\n\u003c/a\u003e\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcodeadamca%2Fjavascript-if-statements","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcodeadamca%2Fjavascript-if-statements","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcodeadamca%2Fjavascript-if-statements/lists"}