1706791852

How to use variables correctly in javascript


* Variables are one of the basic concepts of coding * Using const or let avoid using var * const and let are scope based * Parent scope vs local scope * Assign values to variables and reassign new updated values * Use of strings and numbers as values for variables * Dynamic type with JavaScript changing data type * Math and output from JavaScript directly Variables allow developers to hold values, these values can be updated as needed within the code. Typically, variables are declared at the start of your code, and used throughout the code to hold application values. The values can change using “let” when you declare a variable. Use “const” when you want the value to stay the same throughout the code. This can save errors and issues with reassigning values mistakenly in the code. Double quotes and single quotes or backticks can be used to contain string content. A semicolon is not necessary after a statement if it is written on its own line. But if more than one statement on a line is desired, then they must be separated by semicolons. It is considered best practice, however, to always write a semicolon after a statement, even when it is not strictly needed. Whitespace is ignored in JavaScript code; you can use it for writing code that is more readable and understandable. Creating variables - var - Declares a variable, optionally initializing it to a value. let - Declares a block-scoped, local variable, optionally initializing it to a value. Blocks of code are indicated by {} const - Declares a block-scoped, read-only named constant. Cannot be changed. Variables must be declared before you use them. Comma separate to declare multiple variables. CamelCase is more readable as in the example below. let a,b,c,d; let userfirstname = “Laurence”; let userFirstName = “Laurence”; **Lesson JavaScript Coding Exercise:** 1. Declare variables both numbers and strings using both let and const 2. Try the various formats to create strings using the single, double quotes and backticks 3. Output the results into the console and to the document. 4. Assign new values to the variables and output the results 5. Try the template literal strings with JavaScript wrapped in the ${} and check the output in both the console and webpage ```js let a = 'Laurence Svekis'; let b = 10; let e = 5; let f = 20; let myStr1 = 'Hello\'s"world'"; let myStr2 = "\"Hello\"'world'"; let myStr3 = `Hello' s "world"`; let tempLit = `Math ${b + e + f}Hello`; const myName = 'Laurence Svekis'; //myName = 'Svekis'; { const b = 1000; console.log(b); //var c = 100; //let d = 100; } //console.log(c); console.log(b); console.log(myName); a = 'Hello World'; let val = b + e + f; val = myStr1 +''+myStr2+''+myStr3; val = tempLit; val = `${myStr1}${myStr3}${myStr3}`; console.clear(); console.log(val); document.write(val); ``` Leave a comment if this tip was valuable to you or talk to me in the chatroom [<span style='color: blue;'>here</span>](https://chat-to.dev/chat?q=javascript-room), as this is very important to us and helps us to bring you more and more content like this.

(0) Comments