Unit V - Lesson 2 | Codetantra HTML

Unit V - Lesson 2 | Codetantra HTML

Unit V - Lesson 2

### 6.2.1. Variable declaration & initialization

    // this is a comment in JS.
    // Write your code below this
    var who='World';

6.2.2. Changing variable values

    // write your code below this line
    let greeting = oldGreeting;
    greetingLength= greeting.length;

6.2.3. Unchanging variables

    // 2 initialized variables
    let v3 = 'Hello', v4 = 'World';
    // this is a constant declaration
    const SPACE = ' ';

    // this is a constant variable declaration
    // since it is a constant it cannot be assigned later
    // it must be initialized on declarations.
    // otherwise this causes an error
    // merging the two lines below into a 
    // single declaration with initializer will solve the issue.
    let greeting;
    greeting = v3 + SPACE + v4;

6.2.4. Types in JS

    (c) let v1 = -1.23, v2 = 'Hello';
    v1 = v2 + ' world';

6.2.5. Primitive values in JS (b) If * is the exponentiation operator, the following code results in n being valued Infinity: let n = 2 * 2000;

    6.2.6. Number literals
    let c = 0x11;
    const d = c - 1;
    // value of d is 16 in decimal

6.2.7. String literals

    // 3 variables
    let v1, v2, v3;

    // assigning string literal values to the variables

    // this has an error. fix by escaping the single-quote
    // or by wrapping in double-quotes instead of single-quotes
    v1 = "This is Raj\'s desk";

    // this has an error. fix by representing the newline using \n
    // or by using a template literal
    v2 = `Line 1
    Line 2`;

    // this is assigning a template literal with string interpolation
    // there's no error here. don't change this
    v3 = `${v1}. You can stand in:
    ${v2}`;

6.2.8. Array literals

    // 3 variables
    let v1, v2, v3;

    // assigning the second element of arr to v1. don't change this
    v1 = arr[1];

    // fix below line by changing value of third element of arr to the number 10
    arr[2] = 10;

    // assign the the sum of first & third elements of arr to v2 & fix error in below line
    v2 = arr[0] + arr[2];

    // there is no error here. do not change the below line
    v3 = `${v2} is ${arr[0] + arr[2]}`;