JavaScript: Naming conventions and Strings
Table of Contents
Naming and Naming conventions
- Give variables meaning-full names:
myName,inputAge,userPick. - Variables can’t start with numbers, but can contains numbers.
- Can’t have space inside.
- Only letters, numbers,
$, and_.
Strings
- You can combine strings with
+:
var a = "Hello";
var b = "World";
alert("a" + " " + "b"); // Hello World
alert("a" + " b"); // Hello World
alert("a " + "b"); // Hello World
Challenge
Create a greeting Alert using 2 variables: message and name.
var name = prompt("What is your name?");
var message = "Hello there ";
alert(message + name);
String Lengths
length: The read-only length property of the NamedNodeMap interface is the number of objects stored in the map.
var name = "Moriel";
name.length;
Challenge
Give a prompt, tell how many characters are, and how many left to 140.
My solution:
var userMessage = prompt("Insert your message: ");
var messageLength = userMessage.length;
var leftLength = 140 - messageLength;
alert("You used " + messageLength + " characters, you left with " + leftLength + " characters");
Teacher:
var userMessage = prompt("Insert your message: ");
var messageLength = userMessage.length;
alert("You used " + messageLength + " characters, you left with " + (140 - messageLength) + " characters");
Slicing and extracting parts of a string
slice(x,y): The slice() method of Array instances returns a shallow copy of a portion of an array into a new array object selected from start to end (end not included) where start and end represent the index of items in that array. The original array will not be modified.
Example
var name = "Moriel";
name.slice(0,1); // From position 0 to 1 not included.
// prints: "M"
name.slice(0,3); // From position 0 to 3 not included.
// prints: "Mor"
Challenge
Cut up the message above 140 characters.
var userMessage = prompt("Insert your message: ");
alert(userMessage.slice(0,140));
Can be done in one line:
alert(prompt("Insert message:").slice(0,140));
toUpperCase / toLowerCase
toUpperCase(): The toUpperCase() method of String values returns this string converted to uppercase.
var name = "Moriel";
name.toUpperCase();
// prints: "MORIEL"
// To keep the new upper case as variable:
name = name.toUpperCase();
toLowerCase(): The toLowerCase() method of String values returns this string converted to lower case.
var name = "Moriel";
name.toLowerCase();
// prints: "moriel"
// To keep the new upper case as variable:
name = name.toLowerCase();
Challenge
Ask a prompt for a name, be able to alert “Hello, Name”
var userName = prompt("What is your name?");
var cap0 = userName.slice(0,1);
var restOfName = userName.slice(1);
alert(cap0.toUpperCase() + restOfName.toLowerCase());